0

I am trying to get network mapped drives using below commands.

Get-WmiObject -Class Win32_MappedLogicalDisk |  %{$_.Name}

Get-WmiObject -Class Win32_MappedLogicalDisk |  %{$_.ProviderName}

This works in some system however does not in other systems(may be powershell version issue) So I thought of using net use command. However, I am unable to fetch the values or not sure how to get the values displays when i type 'net use'

when I type net use I get status, Local, Remote and Network column. I tried to use the below command to get the field values.

net use | select local.

but I get blank or nothing

Used below command.

net use | select local.

Need to get Local and Remote values from net use command.

4
  • 3
    NET is not a PowerShell command; it's an external program. As such, it does not pass structured objects into the PowerShell pipe, only text. The Select-Object cmdlet expects a structured object, and will return the named member property from that object - in other words, in your example, it's expecting a structured object with a member property called "local.". You will need to parse the text. Commented May 14, 2019 at 13:29
  • To see everything available, use Get-PSDrive | Get-Member -Force. To get only FileSystem "drives," use Get-PSDrive | Where-Object { $_.Provider.Name -eq 'FileSystem' }. Commented May 14, 2019 at 14:40
  • 1
    Not sure if people missed this comment This works in some system however does not in other systems(may be powershell version issue) so PS might not be the solution and the smells like an XY problem at this point. There is an error or issue we don't see Commented May 14, 2019 at 14:45
  • @lit As per MS documentation on Managing Windows PowerShell Drives you could use Get-PSDrive -PSProvider FileSystem Commented Mar 31, 2021 at 9:57

4 Answers 4

0

See this for parsing legacy console output ---

How to Convert Text Output of a Legacy Console Application to PowerShell Objects

Yet, along with what LotPings gave you already. Your query could be a duplicate of this ... Equivalent of net use (to list computer's connections) in powershell? ... and it's accepted answer

# For the mapped logical drive you can use WMI class Win32_MappedLogicalDisk :

Get-WmiObject Win32_MappedLogicalDisk

# Here is another way with Win32_LogicalDisk :

PS C:\> Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = 4"

DeviceID     : V:
DriveType    : 4
ProviderName : \\jpbdellf1\c$
FreeSpace    :
Size         :
VolumeName   :

# Edited
# You are right, you can get what you need with Win32_NetworkConnection :
Get-WmiObject Win32_NetworkConnection

LocalName                     RemoteName                    ConnectionState               Status
---------                     ----------                    ---------------               ------
                              \\jpbasusf1\temp              Connected                     OK

# On Seven or W2K8 be careful to call this with the same user that run the NET USE because it's a session information.
Sign up to request clarification or add additional context in comments.

Comments

0

How about using get-psdrive (the root header actually matches the displayroot property)?

get-psdrive | where displayroot -like '\\*'

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
Y                  91.84          7.82 FileSystem    \\server....

Comments

0

Depending on the PowerShell versions available you might encounter similar problems with
Get-SmbMapping which wraps the CimClass: ROOT/Microsoft/Windows/SMB:MSFT_SmbMapping.

But has otherwise an output resembling net use.

To process the real net use output and convert to an object with properties,
you may use:

$SmbMapping = (net use) -like '* \\*' | ForEach-Object { 
    $Status,$Local,$Remote,$Null = $_ -split ' +',4
    [PSCustomObject]@{
        Status = $Status
        Local  = $Local
        Remote = $Remote
    }
}

This works at least in my German locale Win10.
(Not sure about different status messages in other locales.)

Comments

0

Update to @postanote's answer.

Get-WmiObject is no longer available on some systems you must now instead use the following PS command to access the API:

Get-CimInstance -ClassName Win32_NetworkConnection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.