PSDrives

PSDrives are the presentation of a connected datastore by a PSProvider. They are repesented by a name.

ProviderDrive(s)
AliasAlias:
CertificateCert:
EnvironmentEnv:
FileSystemC: (*)
FunctionFunction:
RegistryHKLM: HKCU:
VariableVariable:
WSManWSMan:
Default PSProviders with their Name

First of all lets see what PS Drives we have available

Get-PSDrive

Screen capture showing the otput of Get-PSDrive

The capture above shows the PSDrives on my machine and you can see all the defaults listed,

  • Alias
  • FileSystem
  • Certificate
  • Registry
  • Variable
  • Function
  • WSMan.

FileSystem

Let’s start with the file system, this is the most commonly used and probably the most familiar. As you can see below the file system name (in windows) is equal to its drive letter, you can address any drive on the system in PowerShell this way.

Screen capture showing the output of Get-PSProvider but only selecting the Filesystem provider

Names and Aliases

Note how all of the Filesystem drive names are linked to their drive letter (at least in Windows). Now you can use all your old cmd.exe commands in here just as with a traditional file system, things like cd or dir all work

Screen capture showing the use of cd and dir commands in a PowerShell session

what is less obvious here is that these are actually Aliases to PowerShell cmdlets

  • Change directory – cd \ = Set-Location C:\
  • List Directory – dir = Get-ChildItem

you can see all the aliases with the cmdlet Get-Alias but to prove the point here let’s try and find the CD command. You should by now have a good guess that get-alias cd will provide what you need:

Screen capture showing the output of Get-Alias cd

Using the PSDrive

So now we can access the drive using PowerShell cmdlets lets manipulate a file:

New-Item C:\FirstNewFile.txt
Add-Content C:\FirstNewFile.txt "This is the first addition"

cat C:\FirstNewFile.txt 

This creates a new file, adds a line of text to the file and then outputs the contents of the file to standard out (STDOUT). Note that we use the Linux command cat to output the file contents but what are we really doing here? Yes of course we are using an alias, and we can see which one by using the get-alias command Get-Alias cat which exposes the actual PS cmdlet as Get-Content

Screen capture showing output of Get-Alias cat

We can further manipulate the data in the file. We can add additional text by using more add-content cmdlets

Screen capture showing use of add-content cmdlet
Screen capture showing output of Get-Content cmdlet demonstrating the second Add-Content had worked

We can replace the data

set-content C:\FirstNewFile.txt "This is the replacement text"
Screen capture showing the use of Set-Content to overwrite\replace the contents of FirstNewFile.txt

And finally lets remove the file we created using Remove-Item

Remove-Item C:\FirstNewfile.txt
Screen captuire showing the rmeoval of the file FirstNeweFile.txt using the cmdlet Remove-Item

In conclusion then, we can create items, list items, add content to items, replace content in items and remove items. So far we have done this using files, but the whole point of this section is that PSProviders present their connected data stores as drives so we can repeat this exercise on things such as the registry using the same or similar file manipulation cmdlets

Manipulating the Registry

Lets consider how we can look at and manipulate the registry. I have created a dummy registry Key called vroamam at

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\vroamam

Screen Capture showing demo key and values in the windows registry (regedit)

How would you list this key using PowerShell to see what values it held? Remember that we use the same cmdlets as we did for the file system so to list the contents of a folder/diretcory we used Get-Content … we do the same here but with the path to the key instead of the folder.

Get-ChildItem HKLM:\SOFTWARE\vroamam
Screen capture sbhowing output of Get-ChildItem for HKLM:\SOFTWARE\vroamam

Just as we did with the file system content we can replace the content of the Property this time though we have to use a different cmdlet Set-ItemProperty and we provide it with a path to the item, the item name and a value

 set-itemproperty -Path HKLM:\SOFTWARE\vroamam\PowerShellDemo -Name "Demo String" -Value "Replaced Value"
Screen capture showing use of Set-ItemProperty with before and after listings using Get-ChildItem

Usign the cmdlets you can create new items, set new item property values and rmeove items. Just the same way as we did with the file system.

Environment

And finally, to prove the point, you can do the same with other data stores such as environment variables.

Get-ChildItem Env:*
Screen capture showing a list of environment varaiables from Get-ChildItem Env:*

If you want to see the value of a single variable

get-Item env:computername

To set a new value or replace an existing value

set-content -Path PowerShellDemo -Value 'Initial Value'
set-content -Path PowerShellDemo -Value 'Replaced Value'
Screen capture showing the output and operation of Set-Location, Set-Content and Get-Item cmdlets

Conclusion

Hopefully you can see that you can manipulate a variety of system data stores using PowerShell Providers and PowerShell Drives using a simple file system approach. Up Next – Using WMI and CIM