Navigate SQL Server PowerShell Paths


Applies To: SQL Server 2016

The Database Engine PowerShell provider exposes the set of objects in an instance of SQL Server in a structure similar to a file path. You can use Windows PowerShell cmdlets to navigate the provider path, and create custom drives to shorten the path you have to type.

Windows PowerShell implements cmdlets to navigate the path structure that represent the hierarchy of objects supported by a PowerShell provider. When you have navigated to a node in the path, you can use other cmdlets to perform basic operations on the current object. Because the cmdlets are used frequently, they have short, canonical aliases. There is also one set of aliases that maps the cmdlets to similar command prompt commands, and another set for UNIX shell commands.

The SQL Server provider implements a subset of the provider cmdlets, shown in the following table.

cmdletCanonical aliascmd aliasUNIX shell aliasDescription
Get-LocationglpwdpwdGets the current node.
Set-Locationslcd, chdircd, chdirChanges the current node.
Get-ChildItemgcidirlsLists the objects stored at the current node.
Get-ItemgiReturns the properties of the current item.
Rename-ItemrnirnrenRenames an object.
Remove-Itemridel, rdrm, rmdirRemoves an object.
System_CAPS_ICON_important.jpg Important

Some SQL Server identifiers (object names) contain characters that Windows PowerShell does not support in path names. For more information about how to use names that contain these characters, see SQL Server Identifiers in PowerShell.

SQL Server Information Returned by Get-ChildItem

The information returned by Get-ChildItem (or its dir and ls aliases) depends on your location in a SQLSERVER: path.

Path locationGet-ChildItem results
SQLSERVER:\SQLReturns the name of the local computer. If you have used the SMO or WMI to connect to instances of the Database Engine on other computers, those computers are also listed.
SQLSERVER:\SQL\ComputerNameThe list of instances of the Database Engine on the computer.
SQLSERVER:\SQL\ComputerName\InstanceNameThe list of top-level object types in the instance, such as Endpoints, Certificates, and Databases.
Object class node, such as DatabasesThe list of objects of that type, such as the list of databases: master, model, AdventureWorks20008R2.
Object name node, such as AdventureWorks2012The list of object types contained within the object. For example, a database would list object types such as tables and views.

By default, Get-ChildItem does not list any system objects. Use the Force parameter to see system objects, such as the objects in the sys schema.

Custom Drives

Windows PowerShell lets users define virtual drives, which are referred to as PowerShell drives. These map over the starting nodes of a path statement. They are typically used to shorten paths that are typed frequently. SQLSERVER: paths can get long, taking space in the Windows PowerShell window and requiring a lot of typing. If you are going to do a lot of work at a particular path node, you can define a custom Windows PowerShell drive that maps to that node.

Use a cmdlet alias

  • Instead of typing a full cmdlet name, type a shorter alias, or one that maps to a familiar commend prompt command.

Alias Example (PowerShell)

For example, you can use one of the following sets of cmdlets or aliases to retrieve a listing of the SQL Server instances available to you by navigating to the SQLSERVER:\SQL folder and requesting the list of child items for the folder:

## Shows using the full cmdet name.  
Set-Location SQLSERVER:\SQL  
## Shows using canonical aliases.  
## Shows using command prompt aliases.  
## Shows using Unix shell aliases.  

Return information by using Get-Childitem

  1. Navigate to the node for which you want a list of childrem

  2. Run Get-Childitem to get the list.

Get-ChildItem Example (PowerShell)

These examples illustrate the information returned by Get-Childitem for different nodes in a SQL Server provider path.

## Return the current computer and any computer  
## to which you have made a SQL or WMI connection.  
Set-Location SQLSERVER:\SQL  
## List the instances of the Database Engine on the local computer.  
Set-Location SQLSERVER:\SQL\localhost  
## Lists the categories of objects available in the  
## default instance on the local computer.  
Set-Location SQLSERVER:\SQL\localhost\DEFAULT  
## Lists the databases from the local default instance.  
## The force parameter is used to include the system databases.  
Set-Location SQLSERVER:\SQL\localhost\DEFAULT\Databases  
Get-ChildItem -force  

Create and use a custom drive

  1. Use New-PSDrive to define a custom drive. Use the Root parameter to specify the path that is represented by the custom drive name.

  2. Reference the custom drive name in path navigation cmdlets such as Set-Location.

Custom Drive Example (PowerShell)

This example creates a virtual drive named AWDB that maps to the node for a deployed copy of the AdventureWorks2012 sample database. The virtual drive is then used to navigate to a table in the database.

## Create a new virtual drive.  
New-PSDrive -Name AWDB -Root SQLSERVER:\SQL\localhost\DEFAULT\Databases\AdventureWorks2012  
## Use AWDB: to navigate to a specific table.  
Set-Location AWDB:\Tables\Purchasing.Vendor  

SQL Server PowerShell Provider
Work With SQL Server PowerShell Paths
Convert URNs to SQL Server Provider Paths
SQL Server PowerShell

Community Additions