Creating a backup device

This recipe will show how you can create a backup device using PowerShell.

Getting ready

We are going to create a backup device in this recipe. Here's the T-SQL equivalent of what we are trying to accomplish:

EXEC master.dbo.sp_addumpdevice @devtype = N'disk',
    @logicalname = N'Full Backups',
    @physicalname = N'C:\Backup\backupfile.bak'

How to do it...

Let's list the steps required to create a backup device:

  1. Open PowerShell ISE as administrator.
  2. Import the SQLPS module as follows:
    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
  3. Add the following script and run:
    $instanceName = "localhost" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName #this file will be created ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.