In PowerShell, how can I determine if the current

2019-01-26 10:12发布

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:24 H:\temp
>get-psdrive  h

Name       Provider      Root      CurrentLocation
----       --------      ----      ---------------
H          FileSystem    H:\          temp

but in MS-Dos "net use" shows that H: is really a mapped network drive:

New connections will be remembered.

Status       Local     Remote                    Network
-------------------------------------------------------------------------------
OK           H:        \\spma1fp1\JARAVJ$        Microsoft Windows Network

The command completed successfully.

What I want to do is to get the root of the drive and show it in the prompt (see: Customizing PowerShell Prompt - Equivalent to CMD's $M$P$_$+$G?)

6条回答
倾城 Initia
2楼-- · 2019-01-26 10:33

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.

查看更多
戒情不戒烟
3楼-- · 2019-01-26 10:35

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}

查看更多
地球回转人心会变
4楼-- · 2019-01-26 10:35

A slightly more compact variation on the accepted answer:

[System.IO.DriveInfo]("C")
查看更多
ら.Afraid
5楼-- · 2019-01-26 10:40

Use the .NET framework:

PS H:\> $x = new-object system.io.driveinfo("h:\")
PS H:\> $x.drivetype
Network
查看更多
\"骚年 ilove
6楼-- · 2019-01-26 10:45

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

查看更多
Root(大扎)
7楼-- · 2019-01-26 10:52

Try WMI:

Get-WMI -query "Select ProviderName From Win32_LogicalDisk Where DeviceID='H:'"
查看更多
登录 后发表回答