William Bartholomew

Musings on software engineering, technology and Aspergers Syndrome.

Enhancement to PowerShell Drives Script

leave a comment »

I recently posted a script to map local and remote directories to PowerShell drives. Because this script executes when you open a new PowerShell session if you open it while disconnected then any network drives will remain unmapped until you start a new PowerShell session. I’ve resolved this by changing the script to map the drive on the fly when you try to access the drive. Here is the revised script:

$Drives = @{
    "dev"="C:\Dev\";
    "docs"="C:\Users\WilliamB\Documents\";
    "utils"="C:\Utils\";
    "drops"="\\server\drops\"
}

foreach ($Name in $Drives.Keys) {
    $Location = $Drives.$Name

    Invoke-Expression "function global:$($Name): { `
        if ( -not (Get-PSDrive $($Name) -ErrorAction SilentlyContinue) ) {
            `$null = New-PSDrive -PSProvider FileSystem -Name $Name -Root $Location -Scope Global
        }
        Push-Location $($Name): `
    }"
}


Written by wbarthol

August 31, 2009 at 2:40 pm

Posted in sysadmin

Leave a Reply