9

#PSTip Persistent file system drives

In the previous tip we showed you how  to get the next available drive letter using the Get-AvailableDriveLetter function when we want to create a new mapped network drive.

The Get-AvailableDriveLetter function is one of the commands in the Storage module in Windows 8 and Server 2012.

Starting with PowerShell 3.0, we can now map persistent drives using the enhanced New-PSDrive cmdlet. The following command uses the Get-AvailableDriveLetter function together with the New-PSDrive cmdlet and its -Persist parameter to map a persistent drive:

PS> $letter = Get-AvailableDriveLetter -ReturnFirstLetterOnly
PS> New-PSDrive -Name $letter -PSProvider FileSystem -Root \\Server01\Share -Persist

To disconnect the drive:

PS> Remove-PSDrive -Name $letter
Filed in: Columns, Tips and Tricks Tags: , ,

9 Responses to "#PSTip Persistent file system drives"

  1. “The Get-AvailableDriveLetter function is one of the commands in the Storage module in Windows 8 and Server 2012”

    Are you sure about that?

  2. PS C:Windowssystem32> gcm -verb get -Module Storage | % name
    Get-Disk
    Get-DiskImage
    Get-FileIntegrity
    Get-InitiatorId
    Get-InitiatorPort
    Get-MaskingSet
    Get-OffloadDataTransferSetting
    Get-Partition
    Get-PartitionSupportedSize
    Get-PhysicalDisk
    Get-ResiliencySetting
    Get-StorageJob
    Get-StoragePool
    Get-StorageProvider
    Get-StorageReliabilityCounter
    Get-StorageSetting
    Get-StorageSubSystem
    Get-SupportedClusterSizes
    Get-SupportedFileSystems
    Get-TargetPort
    Get-TargetPortal
    Get-VirtualDisk
    Get-VirtualDiskSupportedSize
    Get-Volume
    Get-VolumeCorruptionCount
    Get-VolumeScrubPolicy

  3. Will Smothers says:

    We discovered the -Persist switch with the New-PSDrive command but when we put it in our logon script, the drives map and then once the script finishes running they disappear as if we were using New-PSDrive in Powershell 2.0… Thoughts?

    • ShayLevy says:

      What happens if you manually execute the script after the user has logged on? By the way, why do use Persist in your login script? Next time the script is run it will try to rempa the drive and you may get an error? Do you have error handling in your script that might disconnect the drive when error occurs?

      • Jz215 says:

        I have the same issue in my login script. When I use New-PSDrive with -persist it maps the drives and then they disappear. If I don’t use -persist the drives don’t map at all.

        I have a remove-mappeddrives function that runs prior to the drive mappings. I get no errors. The script works as expected except the drives disappear as soon as it completes.

        Any ideas?

        • ShayLevy says:

          Do you see the same behaviour if you execute the script manually after the user is logged in?

          • Jz215 says:

            Yes, I was testing before we went live. I was off the network so the normal login script didn’t run but on vpn so I had network access. Executed from the ISE and from the console. With the persist switch I could see the drives map and disappear when the script completed. Without the persist switch the drives never mapped.

          • ShayLevy says:

            Is this the case for all workstations? Could there be another process that somehow remove mapped drives?

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: