On Nov 22, 9:37 am, Kris <k...@xxxxxx> wrote:
> Hi,
>
> I experienced problems with following sequence. Suppose you are using a
> removable storage device (I used a small USB hard disc) that uses drive
> letter Z:
>
> - Start PowerShell (1.0) without the device connected
> - test-path Z: returns false as expected
> - Connected the device, it will be mounted as Z:
> - test-path Z: returns true as expected
> - Remove the device again
> - test-path Z: returns again false as expected
>
> Now, re-connect the device once more. Even though the device is still
> perfectly accessible from my computer, PowerShell's Test-Path Z: will
> still return false. Also, accessing drive Z: from within PowerShell by
> directly by navigating to its drive letter returns an error
> ("Set-Location : Cannot find drive. A drive with name 'Z' does not exist.").
>
> From this point on, there's no way anymore to access the Z: drive from
> withing this PowerShell session. I have to start a new PowerShell
> session in order to gain access again to Z:
>
> I was wondering if anyone knows what is happing and how to prevent this?
> Is there a command that allows you to force a re-scan of all drives so
> that all drives are correctly recognized again?
>
> Note that this problem only occurs when you start your PowerShell
> session with the drive disconnected. If drive Z: already exists when
> starting the PowerShell session, the problem does not occur and you can
> connect/disconnect the device as many times as you like without ever
> loosing access.
>
> Thanks,
>
> Kris The answer to this is simple, yet confusing at the same time:
* A powershell drive is not a windows drive.
When powershell starts up, there is a procedure for its filesystem
provider called "initializedefaultdrives." This proc looks at all of
your windows drives and creates a powershell PSDrive for each one with
the same name. The thing is, this is only called _once_; at powershell
startup. This is why your Z: drive does not appear later on. The posh
team as you can see, actually built something quite different than
cmd.exe. But they -- mostly -- did a good job at hiding this for those
of us coming from a traditional DOS-like shell. So, what's the
solution? This:
ps> new-psdrive z -provider filesystem -root z:\
The "root" is the windows drive to point at - the "z" is the name of
the powershell drive you want. You could choose any name you want, and
it will point to windows drive z:\
And just to make matters slightly more confusing, when you type:
ps> c:
....you are actually invoking a function named "C:" which runs the
code:
Set-Location C:
You can verify this for yourself by trying:
ps> get-content function:"c:"
Set-Location C:
Again, this function is not created for you when you map a new "Z"
drive, so typing "z:" and hitting enter will not work until you
additionally perform:
ps> $function:"z:" = { set-location z:\ }
Hope this helps,
- Oisin / x0n
http://www.nivot.org