Windows Vista Forums

Removable drive becomes inaccessible from within powershell eventhough still accessible My Computer
  1. #1


    Kris Guest

    Removable drive becomes inaccessible from within powershell eventhough still accessible My Computer

    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



      My System SpecsSystem Spec

  2. #2


    Oisin Grehan Guest

    Re: Removable drive becomes inaccessible from within powershell eventhough still accessible My Computer

    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





      My System SpecsSystem Spec

  3. #3


    Kris Guest

    Re: Removable drive becomes inaccessible from within powershell eventhough still accessible My Computer

    Oisin Grehan wrote:

    > of us coming from a traditional DOS-like shell. So, what's the
    > solution? This:
    >
    > ps> new-psdrive z -provider filesystem -root z:\
    >
    Thank you for your very clear and comprehensive answer. It all makes
    sense now :-)

    Initially I had a small problem with your example but then I changed
    -provider into -psprovider and everything was fine now.

    Thanks again.

    Kris

      My System SpecsSystem Spec

Removable drive becomes inaccessible from within powershell eventhough still accessible My Computer problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Drive inaccessible Drag General Discussion 0 12 Sep 2009
Trouble booting. Now inaccessible drive. nocker209 Vista hardware & devices 0 14 Jan 2009
Network drive becomes inaccessible when downloading. reddevil Vista networking & sharing 1 26 May 2007
Remote Control Makes Computer Inaccessible Locally Larry Epn Vista performance & maintenance 0 05 Apr 2007
Inaccessible foleders in C: drive churin Vista General 11 30 Nov 2006