在 Powershell 中为 PSDrive 设置名称

Setting a Name for a PSDrive in Powershell

当我创建 PSDrive 时,它​​通常只获取它指向的位置的名称。 "Being dom-loc-Share (G:)"

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

我现在通过单击名称并更改它来命名我的 "Bob"。但是,我现在创建的每个 PSDrive 都称为 Bob

这很奇怪,但不是真正的问题(尽管如果有人愿意解释的话)。 我的问题是:如何在我的脚本中设置名称,即 bob?

我试过了

Get-PsDrive G

并检查了属性,但无法在 windows 资源管理器中设置显示的名称。

如何在创建 PSDrive 时将此名称而不是 bob 设置为 "Group-Drive"?

如果我尝试 Get-WmiObject -Class win32_volume 我会得到以下结果...

你可以这样做:

$drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'g:'"

Set-WmiInstance -input $drive -Arguments @{Label="Bob"}

旁注:

使用Set-WmiInstance 很有用,因为它一次可以更改多个参数;示例:

Set-WmiInstance -input $drive -Arguments @{DriveLetter="Q:"; Label="NewLabel"}

我找到了答案,不过如果有人知道更好的方法,请随时告诉我。

$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"
        }
    }
}