Prof. Powershell

Provide and Prosper: A Look at PowerShell Providers

Here's a rundown of what's important to remember when dealing with SharePoint providers and what you can ignore.

Recently I received a tweet about PowerShell providers. The message came from someone uncertain about what they were, their role in PowerShell and why he should bother with them. That struck me as a fair question that I expect many PowerShell newcomers share. So let's take another look at PowerShell providers and what they bring to the party.

On one hand, you probably don't need to focus too much on providers and instead focus on what you can accomplish. For example, there is a registry provider, but all that really matters is that you can use the same cmdlets you use in the file system. There is no need to learn a different set of commands.

PS C:\> cd scripts
PS C:\Scripts> dir | out-file list.txt
PS C:\scripts> cd hklm:
PS HKLM:\> dir

A provider is a software component that exposes some underlying technology or data storage system to PowerShell. This exposure is most often done through a drive "mapping", we refer to as a PSDrive. Providers are written in a .NET language and are something most IT Pros consume rather than create. ou use providers all the time and maybe don't realize it.

For example, when I have a prompt like this:

PS C:\work>

I am using the FileSystem provider and the C PSDrive. I change directories to another PSDrive just as if it was part of the file system.

 

PS C:\work> cd HKCU:
PS HCKU:\>

Out of the box, PowerShell comes with several default providers which you can see with the Get-PSProvider cmdlet.

PS C:\> get-psprovider

Name                 Capabilities                                      Drives   
----                 ------------                                      ------   
Alias                ShouldProcess                                     {Alias}  
Environment          ShouldProcess                                     {Env}    
FileSystem           Filter, ShouldProcess, Credentials                {C, D, E,...}
Function             ShouldProcess                                     {Function}
Registry             ShouldProcess, Transactions                       {HKLM, HKCU}
Variable             ShouldProcess                                     {Variable} 
Certificate          ShouldProcess                                     {Cert}   
WSMan                Credentials                                       {WSMan}  

You might get additional providers depending on what module you import. For example, when you import the Active Directory module you will get an AD: PSDrive. Almost all PowerShell providers that I have seen also have a help file. All you have to do is ask for help on the provider name.

PS C:\> help registry

Unfortunately, there is a bug with the –Showwindow parameter introduced in PowerShell v3. The following command will run, but the output will be malformed:

PS C:\> help registry -showwindow

The final, and what I find the most interesting feature, is that many cmdlets are provider aware. For example on a v3 or later system run this command from the C: drive.

Help get-childitem

Or you can use the –Path parameter with Get-Help.

Help get-childitem –path cert

You will see a parameter set that includes –File and –Directory. Change to a registry drive and try the help command again. That parameter set is gone! It has no meaning in the registry so it isn't displayed. Now change to the CERT: PSDrive and try one more time. Now there are parameters for –CodeSigning and –ExpiringDays. Some cmdlet help examples also change depending on what provider you are in. What this means is that you should look at full help and examples for cmdlets you are trying to use in a given PSDrive with a PSProvider. There just might be something there that is just what you need.

The great part of a Provider is that it can extend what you can do with PowerShell using the same commands, syntax and language elements that you already know. So my advice is to not worry too much about what a Provider is but rather focus more on how it can make you prosper.

Next time we'll look at working with PSDrives in a bit more detail

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events