powershelldrive

Setting a Name for a PSDrive in Powershell


when I create a PSDrive it usually just gets the name of the location it points to. "Being dom-loc-Share (G:)"

New-PSDrive -Description "Group-Drive" –Name "G" –PSProvider FileSystem –Root "\\dom\dfs\dom-loc-Share" –Persist | Out-Null 

Screen

I named mine "Bob" now by clicking the name and changing it. However every PSDrive I create now is called Bob

This is curious but not the real issue (though if someone cared to explain). My question is: How do I set the name i.e. bob in my script?

I tried

Get-PsDrive G

and checked the properties, but was not able to set the displayed name in the windows explorer.

How would I set this name instead of bob to "Group-Drive" upon creation of the PSDrive?

If I try Get-WmiObject -Class win32_volume I get the following result...

enter image description here


Solution

  • I found the answer, though if anyone knows a better approach to it, feel free to tell me.

    $DNsplit = ((Get-ADUser ([Environment]::UserName)).DistinguishedName).split(",")
    $STO = $DNsplit[$DNsplit.count -5].Substring(3,3)
    $MyRoot = "\\dom\dfs\dom-$Sto-Share"
    $RegPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2"
    if(Test-Path $MyRoot)
    {
        if(Get-PSDrive | ?{$_.Name -eq "G"}) 
        {
            Remove-PSDrive G
        }
        New-PSDrive –Name G –PSProvider FileSystem –Root $MyRoot –Persist -Scope Global | Out-Null 
        $a = Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2"
        foreach($Key in $a)
        {
            $CompKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\$($MyRoot.Replace('\','#'))"
            if($Key.Name -eq $CompKey)
            {
                Set-ItemProperty -Path $key.PSPath -Name "_LabelFromReg" -Value "Group-Drive"
            }
        }
    }