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.
UNIX shell alias
Gets the current node.
Changes the current node.
Lists the objects stored at the current node.
Returns the properties of the current item.
Renames an object.
Removes an object.
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.
The information returned by Get-ChildItem (or its dir and ls aliases) depends on your location in a SQLSERVER: path.
Returns 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.
The list of instances of the Database Engine on the computer.
The list of top-level object types in the instance, such as Endpoints, Certificates, and Databases.
Object class node, such as Databases
The list of objects of that type, such as the list of databases: master, model, AdventureWorks20008R2.
Object name node, such as AdventureWorks2012
The 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.
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.
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 Get-ChildItem ## Shows using canonical aliases. sl SQLSERVER:\SQL gci ## Shows using command prompt aliases. cd SQLSERVER:\SQL dir ## Shows using Unix shell aliases. cd SQLSERVER:\SQL ls
Return information by using Get-Childitem
Navigate to the node for which you want a list of childrem
Run Get-Childitem to get the list.
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 Get-ChildItem ## List the instances of the Database Engine on the local computer. Set-Location SQLSERVER:\SQL\localhost Get-ChildItem ## Lists the categories of objects available in the ## default instance on the local computer. Set-Location SQLSERVER:\SQL\localhost\DEFAULT Get-ChildItem ## 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
Use New-PSDrive to define a custom drive. Use the Root parameter to specify the path that is represented by the custom drive name.
Reference the custom drive name in path navigation cmdlets such as Set-Location.
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