This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
You might have noticed that you can change drive in MSH by just typing the driveletter and : and hit enter (e.g. A: c: as we would expect, same as in CMD) but this does not work for the non-disk providers as HKLM: Functions: etc.
as you want to do a LS in the Function:-"disk"
You have to do like this (for now) :
cd function:
ls
as just using Fuction: [enter] does not work :
MSH>function:
'function:' is not recognized as a Cmdlet, function, operable program, or script file.
Another interesting thing you see when you do the LS is that this (the DriveLetters) are only functions that call Set-Location :
if you look in Profile.MSH you will find this piece of Code :
& {
for ($i = 0; $i -lt 26; $i++)
{
$funcname = ([System.Char]($i+65)) + ':'
$str = "function global:$funcname { set-location $funcname } "
invoke-command $str
}
}
This makes a loop trough the alphabet from A-Z and Makes the Functions :
so if we do the Same for the get_drive output, we get All the Providers (-1) as driveletter-functions :
get-drive | forEach {$str = "function global:$($_.name): { set-location $($_.name): } ";invoke-command $str}
so we can change to All providers like this (as my PWD is in the ConsoleTitle you do not see the change but it changed ;-)):
MSH>env:
MSH>hklm:
That is All but one, the function is there but it will not work :
Function Variable: set-location Variable:
MSH>variable:
'variable:' is not recognized as a Cmdlet, function, operable program, or script file.
*caution*
You might think you can leave out the original Code now (as we create the available driveletters too, so existing drive are created double, and non-existing drive are not there so why bother adding them), but if you think about it.. , what will happen when you add your USB-disk, you get a new drive-letter but It will not be added to the functions till you start MSH again, so that's why the are all created infront.
Also Place the New line of code inside of the & {} block (new scope) to keep your environment Clean, But outside the For Loop (as we only want to do this once, not 26 times) to make this more clear I pasted the Resulting Script-block below.
gr /\/\o\/\/
& {
for ($i = 0; $i -lt 26; $i++)
{
$funcname = ([System.Char]($i+65)) + ':'
$str = "function global:$funcname { set-location $funcname } "
invoke-command $str
}
get-drive | forEach {$str = "function global:$($_.name): { set-location $($_.name): } ";invoke-command $str}
}