Decode-SqlName Returns the original SQL Server identifier when given an identifier that has been encoded into a format usable in PowerShell paths. Decode SqlName Returns the un-encoded SQL Server identifier when given an identifier that has been encoded into a format usable in PowerShell paths. SQL Server delimited identifiers can contain special characters not normally supported in PowerShell object names. These extended characters must be either encoded to their hexadecimal representation or escaped using the ` character. Certain characters, such as the colon character (:) cannot be escaped. The hexadecimal encoding is in the format %nn. Decode-SqlName converts the following encodings to the corresponding characters: %5C-\ %3A-: %2E-. %2F-/ %25-% %3C-< %3E-> %2A-* %3F-? %5B-[ %5D-] %7C-| Decode-SqlName SqlName The SQL Server identifier to be reformatted. string SqlName The SQL Server identifier to be reformatted. string string String String -------------------------- EXAMPLE 1 -------------------------- C:\PS> Decode-SqlName -SqlName "My%3ATable`/" My:Table/ Description ----------- Decodes a SQL Server identifier that has an encoded hexadecimal representation for the : character. PowerShell also removes the escaping back-tick character (`) from an escaped / character. SQL Server Books Online: Using SQL Server Identifiers in PowerShell Encode-SqlName Encode-SqlName Encodes extended characters in SQL Server names to formats usable in PowerShell paths. Encode SqlName Encodes special characters in SQL Server names to formats usable in PowerShell paths. SQL Server delimited identifiers can contain characters not normally supported in PowerShell object names. When using delimited identifiers in SQL Server provider paths, these extended characters must be either encoded to their hexadecimal representation or escaped using the ` character. Certain characters, such as the colon character (:) cannot be escaped. The hexadecimal encoding for the characters is in the format %nn. The characters encoded by Encode-SqlName are: \:./%<>*?[]| Encode-SqlName SqlName The SQL Server identifier to be encoded. string SqlName The SQL Server identifier to be encoded. string string String String -------------------------- EXAMPLE 1 -------------------------- C:\PS> Encode-SqlName -SqlName "My:Table/" My%3ATable%2F Description ----------- Encodes a SQL Server table name that contains : and / characters. SQL Server Books Online: Using SQL Server Identifiers in PowerShell Decode-SqlName Convert-UrnToPath Converts a SQL Server Management Object URN to a SQL Server PowerShell provider path. Convert UrnToPath Converts a SQL Server Management Object Uniform Resource Name (URN) to a SQL Server provider path. SQL Server Management Objects have a Urn property that returns a string indicating their location in the SQL Server object hierarchy. If nodes in the Urn are SQL Server delimited identifiers that have extended characters not supported in PowerShell path nodes, the extended characters are encoded with their hexadecimal representation. For example, a table name "My:Table" is encoded as "My%3ATable". Convert-UrnToPath Urn A character string containing a SQL Server Unique Resource Name (URN). A SQL Server URN is a string that identifies the location of an object in the SQL Server hierarchy. URNs have the same information as a SQL Server PowerShell path, only in a different format. SQL Server Management Objects have a Urn property. string Urn A character string containing a SQL Server Unique Resource Name (URN). A SQL Server URN is a string that identifies the location of an object in the SQL Server hierarchy. URNs have the same information as a SQL Server PowerShell path, only in a different format. SQL Server Management Objects have a Urn property. string string String A character string containing a SQL Server Management Object URN. String A character string containing a SQL Server PowerShell provider path. -------------------------- EXAMPLE 1 -------------------------- C:\PS> Set-Location SQLSERVER:\SQL\MyComputer\MyInstance\Databases\AdventureWorks2014 Convert-UrnToPath -Urn (Get-Item .).Urn.ToString() SQLSERVER:\SQL\MyComputer\MyInstance\Databases\AdventureWorks2014 Description ----------- Returns a character string containing the current path. The example uses the ToString() function of the SMO Urn property to return the Urn as a character string. -------------------------- EXAMPLE 2 -------------------------- C:\PS> Set-Location ( Convert-UrnToPath -Urn "Server[@Name='MyComputer']/Database[@Name='AdventureWorks']/Table[@Name='Address' and @Schema = 'Person']" ) Description ----------- Sets the path to the location specified in a SQL Server Management Object URN. -------------------------- EXAMPLE 3 -------------------------- C:\PS> Set-Location SQLSERVER:\SQL\MyComputer\DEFAULT\Databases foreach ($Item in Get-ChildItem) { $Item.Urn.ToString() | Convert-UrnToPath} SQLSERVER:\SQL\MyComputer\DEFAULT\Databases\AdventureWorks2014 Description ----------- Returns a set of character strings, each containing the path to a database in the default instance. Uses a pipeline to pass the current node URN to Convert-UrnToPath Decode-SqlName Encode-SqlName SQLServer SQLSERVER Access SQL Server instances and their databases. The Microsoft SQL Server PowerShell provider lets you manage the configuration of instances of the SQL Server Database Engine and the objects in those instances. The SQL Server PowerShell provider implements separate path hierarchies for different classes of objects that are supported by the SQL Server management object models. The SQL Server PowerShell provider implements one virtual drive named SQLSERVER:. The SQLSERVER: drive has several folders: \SQL, \SQLPolicy, \SQLRegistration, \Utility, \DAC, \DataCollection, \IntegrationServices, and SQLAS. The SQLServer:\SQL folder lets you navigate to instances of the SQL Server Database Engine and manage the objects in the instances, such as logins, tables, functions, jobs, queues, and mail agents. The SQL folder operates using the objects in these namespaces: Microsoft.SqlServer.Management.SMO Microsoft.SqlServer.Management.SMO.Agent Microsoft.SqlServer.Management.SMO.Broker Microsoft.SqlServer.Management.SMO.Mail The SQLSERVER:\SQLPolicy folder lets you navigate to instances of the Database Engine that contains a Policy-Based Management policy store and manage objects such as policies and facets. The SQLPolicy folder operates using the objects in the Microsoft.SqlServer.Management.Dmf and Microsoft.SqlServer.Management.Facets namespaces. The SQLSERVER:\SQLRegistration folder lets you navigate to instances of the Database Engine and manage Registered Server objects such as server groups and registered servers. The SQLRegistration folder operates using the objects in the Microsoft.SqlServer.Management.RegisteredServers and Microsoft.SqlServer.Management.RegSvrEnum namespace. The SQLSERVER:\Utility folder lets you navigate to the managed instances and data-tier applications registered in the SQL Server Utility. The Utility folder operates using the objects in the Microsoft.SqlServer.Management.Utility namespace. The SQLServer:\DAC folder lets you navigage through data-tier applications objects such as DAC package files. The DAC folder operates using the objects in the Microsoft.SqlServer.Management.DAC namespace. The SQLServer:\DataCollection folder lets you navigate to instances of the Database Engine and manage Data Collector objects such as collection sets and configuration stores. The DataCollection folder operates using the objects in the Microsoft.SqlServer.Management.Collector namespace. The SQLServer:\IntegrationServices folder lets you manage SQL Server Integration Services objects such as projects, packages, and environments. The IntegrationServices folder operates using the Microsoft.SqlServer.Management.IntegrationServices namespace. The SQLServer:\SQLAS folders lets you manage SQL Server Analysis Services objects such as aggregations, dimensions, and cubes. The SQLAS folder operates using the Microsoft.AnalysisServices namespace. You can navigate the SQLSERVER: paths by using PowerShell path cmdlets such as Get-Location, Set-Location, and Get-ChildItem. The leading part of a SQLSERVER:\SQL path has the form SQLSERVER:\SQL\ComputerName\InstanceName. You must always specify an instance name, even for default instances. Specify DEFAULT for default instance names. The path nodes following the InstanceName alternate between object classes and object names. Schemas are not represented as an object class. When you specify a path node for top level objects in schemas, such as tables or views, the node name must be in the format SchemaName.ObjectName. For example, this is the path to the AddressID column in the Address table of the Person schema: SQLSERVER:\SQL\MyComputer\DEFAULT\Databases\AdventureWorks\Tables\Person.Address\Columns\AddressID To save typing, you can create your own PowerShell drives that map over the first several nodes of an SQL path. The information that is returned by Get-ChildItem depends on which part of the path the cmdlet is executed from: Returns the local computer name and the list of computers to which you have opened Database Engine connections by using WMI: PS SQLSERVER:\SQL> Get-ChildItem Returns the list of instances on the specified computer: PS SQLSERVER:\SQL\ComputerName> Get-ChildItem Returns the list of top-level object types from the instance, such as Logins and Databases: PS SQLSERVER:\SQL\ComputerName\InstanceName> Get-ChildItem. With the SQL Server PowerShell provider, you manage the objects that are exposed in SQL Server paths by using the SQL Server management object model methods and properties for that class of object. For example, you can use the methods of the Microsoft.SQLServer.Management.SMO.Database class to perform actions such as creating, altering, and dropping databases. You can also use the class properties to query the current state of the objects. Use the Get-Member cmdlet to get a list of the methods and properties that are supported for the various objects. For information about the classes, see the namespace reference sections in the SQL Server Books Online. You cannot select, insert, update, or delete data in tables and views by using the SQL Server PowerShell provider. For example, there is no Select() or Insert() method for tables. Use the Invoke-Sqlcmd cmdlet to access and change the data in tables and views. By default, the SQL Server PowerShell provider opens Windows Authentication connections by using the credentials of the Windows account that is running the PowerShell session. See SQL Server Books Online for information about opening a SQL Server Authentication connection using the provider. In complex databases, the lists returned by Get-ChildItem and the PowerShell tab-completion lists can be very large. You can use three system variables created by the SQL Server PowerShell provider to control the size of these lists: $SqlServerMaximumTabCompletion = n n is an integer that specifies the maximum number of objects in a tab completion list. The default is 0, which means all objects are returned. $SqlServerMaximumChildItems = n n is an integer that specifies the maximum number of objects returned when you run Get-ChildItem on SQL Server path nodes. The default is 0, which means all objects are returned. $SQLServerIncludeSystemObjects = { $True | $False } Specifies whether SQL Server system objects are returned by Get-ChildItem or included in tab-completion lists. The default is $True SQL Server delimited identifiers are object names that are enclosed in brackets ([Table:Name]) or quotation marks ("View>Name"). Delimited identifiers can contain any Unicode character. The special characters in these names must be either encoded to their hexadecimal representation or escaped for use in PowerShell paths. The hexadecimal encoding for characters uses the format %nn. An example of encoding the colon (:)character in the name "Table:Name" is: Set-Location "Table%3AName". The PowerShell escape character is the back tick (`) character that is typically on the key at the top left of the keyboard. An example of escaping an extended character is as follows: Set-Location "View`>Name" Certain characters, such as the colon character (:), cannot be escaped. They must be encoded. The SQL Server PowerShell provider snapin implements two cmdlets to help encode and decode delimited identifiers: Encode-SqlName and Decode-SqlName. You must supply a registry or directory object for the -Type parameter when using these core cmdlets with a SQLRegistration: drive: Copy-Object, Move-Object, New-Object, Remove-Object, and Rename-Object. Navigating SQLSERVER:\SQL paths -------------------------- EXAMPLE 1 -------------------------- This example uses the Set-Location cmdlet to change the path to the SQL folder. Set-Location SQLSERVER:\SQL -------------------------- EXAMPLE 2 -------------------------- This example uses Set-Location to change the path to a default instance of the Database Engine Set-Location SQLSERVER:\SQL\MyComputer\DEFAULT -------------------------- EXAMPLE 3 -------------------------- This example uses Set-Location to change the path to the AdventureWorks sample database. Set-Location SQLSERVER:\SQL\MyComputer\MyInstance\Databases\AdventureWorks Build a New PowerShell Drive -------------------------- EXAMPLE 1 -------------------------- If you are going to do a lot of work in the AdventureWorks database, create a new AWDB: drive to save typing. New-PSDrive -Name AWDB -Root SQLSERVER:\SQL\MyComputer\DEFAULT\Databases\AdventureWorks -PSProvider SQLServer Get the Methods and Properties of a SQL Server Object -------------------------- EXAMPLE 1 -------------------------- This command sets a variable to the SMO Database class and then gets a list of the methods and properties supported by the class. $MyVar = New-Object Microsoft.SqlServer.Management.SMO.Database $MyVar | Get-Member -Type Methods $MyVar | Get-Member -Type Properties -------------------------- EXAMPLE 2 -------------------------- This command navigates to the database node in an SQL-based path and get s a list of the properties that are supported for databases. Set-Location SQLSERVER:\SQL\MyComputer\DEFAULT\Databases Get-Item . | Get-Member -Type Properties Filter SQL Server Objects by Using SMO Properties -------------------------- EXAMPLE 1 -------------------------- This command lists only the tables that are in the AdventureWorks Sales schema. Set-Location SQLSERVER:\SQL\MyComputer\DEFAULT\Databases\AdventureWorks\Tables Get-ChildItem | WHERE {$_.Schema -eq "Sales"} Generate Scripts That Create SQL Server Objects -------------------------- EXAMPLE 1 -------------------------- This script creates a Views.sql file that contains the CREATE VIEW statements that are required to recreate all of the views that are defined in AdventureWorks. Remove_Item c:\PowerShell\Views.sql Set-Location SQLSERVER:\SQL\MyComputer\DEFAULT\Databases\AdventureWorks\Views foreach ($Item in Get-ChildItem) {$Item.Script() | Out-File -Filepath c:\PowerShell\Views.sql -append } Use SMO to Manage SQL Server Objects -------------------------- EXAMPLE 1 -------------------------- This script uses SMO to create a database and then query its state. Set-Location SQLSERVER:\SQL\MyComputer\DEFAULT\Databases $MyVar = New-Object Microsoft.SqlServer.Management.SMO.Database $MyVar.Parent = (Get-Item ..) $MyVar.Name = "NewDB" $MyVar.Create() $MyVar.State Controlling SQL Server Objects in Tab-completion and Get-ChildItem -------------------------- EXAMPLE 1 -------------------------- This script sets the values for the system variables that control the number of SQL Server objects included in tab-completion lists or returned by Get-ChildItem. $SqlServerMaximumTabCompletion = 20 $SqlServerMaximumChildItems = 10 $SqlServerIncludeSystemObjects = $False dir variable:sqlserver* about_provider Encode-SqlName Decode-SqlName Invoke-Sqlcmd New-PSDrive Get-Member Invoke-PolicyEvaluation