In PowerShell, how can I determine if the current drive is a networked drive or not?

前端 未结 6 1120
忘掉有多难
忘掉有多难 2020-12-19 06:08

I need to know, from within Powershell, if the current drive is a mapped drive or not.

Unfortunately, Get-PSDrive is not working \"as expected\":

PS:         


        
相关标签:
6条回答
  • 2020-12-19 06:39

    Use the .NET framework:

    PS H:\> $x = new-object system.io.driveinfo("h:\")
    PS H:\> $x.drivetype
    Network
    
    0 讨论(0)
  • 2020-12-19 06:39

    The most reliable way is to use WMI

    get-wmiobject win32_volume | ? { $_.DriveType -eq 4 } | % { get-psdrive $_.DriveLetter[0] } 
    

    The DriveType is an enum wit hthe following values

    0 - Unknown 1 - No Root Directory 2 - Removable Disk 3 - Local Disk 4 - Network Drive 5 - Compact Disk 6 - RAM Disk

    Here's a link to a blog post I did on the subject

    0 讨论(0)
  • 2020-12-19 06:46

    A slightly more compact variation on the accepted answer:

    [System.IO.DriveInfo]("C")
    
    0 讨论(0)
  • 2020-12-19 06:47

    Take this a step further as shown below:

    ([System.IO.DriveInfo]("C")).Drivetype
    

    Note this only works for the the local system. Use WMI for remote computers.

    0 讨论(0)
  • 2020-12-19 06:48

    An alternative way to use WMI:

    get-wmiobject Win32_LogicalDisk | ? {$_.deviceid -eq "s:"} | % {$_.providername}

    Get all network drives with:

    get-wmiobject Win32_LogicalDisk | ? {$_.drivetype -eq 4} | % {$_.providername}

    0 讨论(0)
  • 2020-12-19 06:51

    Try WMI:

    Get-WMI -query "Select ProviderName From Win32_LogicalDisk Where DeviceID='H:'"
    
    0 讨论(0)
提交回复
热议问题