PSDrives are the presentation of a connected datastore by a PSProvider. They are repesented by a name.
First of all lets see what PS Drives we have available
The capture above shows the PSDrives on my machine and you can see all the defaults listed,
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.
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
dir all work
what is less obvious here is that these are actually Aliases to PowerShell cmdlets
- Change directory –
- List Directory –
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:
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
We can further manipulate the data in the file. We can add additional text by using more
We can replace the data
set-content C:\FirstNewFile.txt "This is the replacement text"
And finally lets remove the file we created using
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
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.
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"
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.
And finally, to prove the point, you can do the same with other data stores such as environment variables.
If you want to see the value of a single variable
To set a new value or replace an existing value
set-content -Path PowerShellDemo -Value 'Initial Value' set-content -Path PowerShellDemo -Value 'Replaced Value'
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