Using Data Stores and PowerShell Providers

Today's modern computing world makes use of a vast variety of data stores, including the file store, the certificate store, the Registry, etc. With previous tools, each of these data stores are accessed using different and incompatible tools, making it hard for the admin to learn, use, and master. PowerShell take a different approach through the use of providers and standard cmdlets.

A PowerShell provider is a software component that sits between the actual data store and the standard cmdlets. The standard cmdlets call into the provider to return the relevant data and present it in a admin-friendly way. To aid in conversion, PowerShell provides aliases for these standard cmdlets. The provider architecture is shown at http://msdn2.microsoft.com/en-us/library/ms714658.d2eb7674-3a27-4baf-91b7-b8eaf1e8ab2c(en-us,VS.85).gif.

To illustrate this, let's look at the two data stores, the filesystem and the Registry:

PSH [D:\foo]:dir

    Directory: Microsoft.PowerShell.Core\FileSystem::D:\foo

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        14/10/2007     12:04            bar

PSH [D:\foo]: cd bar
PSH [D:\foo\bar]: ls

    Directory: Microsoft.PowerShell.Core\FileSystem::D:\foo\bar

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        14/10/2007     12:04          8 foobar.txt

PSH [D:\foo\bar]: cd hklm:
PSH [HKLM:\]: ls

   Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE

SKC  VC Name                           Property
---  -- ----                           --------
  4   0 HARDWARE                       {}
 87   0 SOFTWARE                       {}
  8   0 SYSTEM                         {}


PSH [HKLM:\]: dir .\software\microsoft\powershell

   Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\microsoft\
powershell

SKC  VC Name                           Property
---  -- ----                           --------
  4   2 1                              {Install, PID}

In this example, we use both the Dir and LS aliases to examine two separate data stores: the filesystem and the Registry. Both aliases are to the Get-ChildItem standard cmdlet, which allows both Linux/Unix and Dos/Windows users to use familiar names to perform common and familiar operations. There are several more standard cmdlets:

Clear-Item

Deletes the contents of an item, but does not delete the item.

Clear-ItemProperty

Deletes the value of a property, but does not delete the property itself.

Copy-Item

Copies an item from one location to another within a namespace.

Copy-ItemProperty

Copies a property and value from a specified location to another location.

Get-ChildItem

Gets the items and child items in one or more specified locations.

Get-ItemProperty

Retrieves the properties of a specified item.

Get-Item

Gets the item at the specified location.

Invoke-Item

Invokes the provider-specific default action on the specified item.

Move-Item

Moves an item from one location to another.

New-Item

Creates a new item in a namespace.

Move-ItemProperty

Moves a property from one location to another.

Rename-Item

Renames an item in a Windows PowerShell provider namespace.

New-ItemProperty

Sets a new property of an item at a location.

Remove-Item

Deletes the specified items.

Remove-ItemProperty

Deletes the property and its value from an item.

Rename-ItemProperty

Renames a property of an item.

Set-Item

Changes the value of an item to the value specified in the command.

Set-ItemProperty

Sets the value of a property at the specified location.

This list represents a number of new cmdlets for the admin to learn. In order to minimise the learning curve, PowerShell provides common aliases for each of these standard cmdlet, e.g., Get-ChildItem is aliased to GCI, DIR, and LS. You can get a list of these aliases as follows:

PSH [D:]:get-alias * | where {$_.definition -like "*item*"} | ft -auto

CommandType Name  Definition
---------- ----  ----------
Alias       cli   Clear-Item
Alias       clp   Clear-ItemProperty
Alias       cpi   Copy-Item
Alias       cpp   Copy-ItemProperty
Alias       gci   Get-ChildItem
Alias       gi    Get-Item
Alias       gp    Get-ItemProperty
Alias       ii    Invoke-Item
Alias       mi    Move-Item
Alias       mp    Move-ItemProperty
Alias       ni    New-Item
Alias       ri    Remove-Item
Alias       rni   Rename-Item
Alias       rnp   Rename-ItemProperty
Alias       rp    Remove-ItemProperty
Alias       si    Set-Item
Alias       sp    Set-ItemProperty
Alias       cp    Copy-Item
Alias       ls    Get-ChildItem
Alias       mv    Move-Item
Alias       rm    Remove-Item
Alias       rmdir Remove-Item
Alias       copy  Copy-Item
Alias       del   Remove-Item
Alias       erase Remove-Item
Alias       move  Move-Item
Alias       rd    Remove-Item
Alias       ren   Rename-Item

These aliases enable you to start using PowerShell and PowerShell providers straight away using the names of the DOS/Windows/Unix/Linux commands you are already familiar with. They also avoid you needing to learn new cmdlets in order to get any value from PowerShell.

The aliases and standard cmdlets call into a provider to obtain the relevant information (the contents of the current working file store folder, the current node in the Registry, etc.). PowerShell comes with several providers, and you can add more. Providers built into PowerShell include Alias, Environment, FileSystem, Function, Registry, Variable, and Certificate. Additional third-party providers include SQL Server, One-Note and Microsoft Compute Cluster.

With the provider, you can use the New-PSDrive to create new drives. These drives can then be used as normal drives in the standard cmdlets and related aliases. For example:

PSH [D:\foo]:New-PSDrive -name scripts -psProvider Filesystem -root d:\foo

Name       Provider      Root
----       --------      ----
scripts    FileSystem    D:\foo

PSH [D:\foo]: dir scripts:

    Directory: Microsoft.PowerShell.Core\FileSystem::D:\foo
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        14/10/2007     12:04            bar

In this manner, you can create new drives that are more meaningful than C: and D:. To see what drives are available, you can use the Get-PSDrive cmdlet as shown here (note the new scripts drive we created in the previous example):

PSH [D:\foo]: Get-PSDrive

Name       Provider      Root
----       --------      ----
Alias      Alias
C          FileSystem    C:\
cert       Certificate   \
D          FileSystem    D:\
E          FileSystem    E:\
Env        Environment
F          FileSystem    F:\
Feed       FeedStore
Function   Function
HKCU       Registry      HKEY_CURRENT_USER
HKLM       Registry      HKEY_LOCAL_MACHINE
L          FileSystem    L:\
R          FileSystem    R:\
S          FileSystem    S:\
scripts    FileSystem    D:\foo
T          FileSystem    T:\
U          FileSystem    U:\
Variable   Variable
Y          FileSystem    Y:\

With these drives, you can perform actions like Dir ENV: to get a list of environment variables or dir Variable: to see a list of variables in use. For more help on providers, type Get-Help about_providers at the PowerShell prompt. And for more detailed information about providers and how to build one, see http://msdn2.microsoft.com/en-us/library/ms714636.aspx.