VBScript to Get List of Network Shares using WMI Service

Description

In this article, I am going write VBScript code to find and get list of Network Shares/Share Folders in Local Machine and Remote Server using VBScript by different methods.

Summary

VBScript to List Network Shares in Local Computer

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: ListNetworkSharesinLocalComputer.vbs
3. Run usage in CMD:

C:> CScript <vbscript file path>

Example: CScript C:ScriptsListNetworkSharesinLocalComputer.vbs 
Run the above command to list Network Share Folders with VBScript using WMI Service in Local Computer

Click to get VBScript source code as a file Download ListNetworkSharesinLocalComputer.vbs

 
' ListNetworkSharesinLocalComputer.vbs
' Sample VBScript code with WMI Service to find and list Network Shares .
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService= objSWbemLocator.ConnectServer(".", "rootcimv2")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

For each objShare in colShares

    Wscript.Echo "Name: " & objShare.Name  & vbCrLf _
                       & "Path: " & objShare.Path & vbCrLf  _ 
                       & "Type: " & objShare.Type  & vbCrLf _
                       & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
                       & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
                       & "Caption: " & objShare.Caption   
Wscript.Echo "---------------------------------"
Next

' Wscript.Echo "Network Share Folders in Local computer listed successfully"

WScript.Quit
VBScript to Get List of Network Shares/Share Folders using WMI Service



Note: Here, I have filtered Win32_Share collections by its Type=0 to list only Share Folders. You can also filter by Type value other than 0 to get other shared items like Printers, Devices, IPC, and etc…Check the following Type values for corresponding Shared Items.

Shared Item Type
Disk Drive 0 (0x0)
Print Queue 1 (0x1)
Device 2 (0x2)
IPC 3 (0x3)
Disk Drive Admin 2147483648 (0x80000000)
Print Queue Admin 2147483649 (0x80000001)
Device Admin 2147483650 (0x80000002)
IPC Admin 2147483651 (0x80000003)

VBScript to Get Network Shares/Share Folders  in Remote Server

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Change the value of string variable ‘strComputer’ to your own Computer or Server name.
3. Save the file with a .vbs extension, for example: ListNetworkSharesinRemoteServer.vbs
3. Run usage in CMD:

C:> CScript <vbscript file path>

Example: CScript C:ScriptsListNetworkSharesinRemoteServer.vbs 
Run this command from Command Prompt to get list of Network Shares with VBScript using WMI Service in Remote Server.

Click to get VBScript source code as a file Download ListNetworkSharesinRemoteServer.vbs

 
' ListNetworkSharesinRemoteServer.vbs
' Sample VBScript to find and list Network Shares in Remote Machine .
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------
strComputer = "hp-PC"
' you can change your own Computer or Server.

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService= objSWbemLocator.ConnectServer(strComputer , "rootcimv2")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

For each objShare in colShares

    Wscript.Echo "Name: " & objShare.Name  & vbCrLf _
                       & "Path: " & objShare.Path & vbCrLf  _ 
                       & "Type: " & objShare.Type  & vbCrLf _
                       & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
                       & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
                       & "Caption: " & objShare.Caption   
Wscript.Echo "---------------------------------"
Next

' Wscript.Echo "Network Share Folders in Remote Computer listed successfully"

WScript.Quit

Find Network Share Folders in VBScript by using other User Credential

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Change the value of string variable ‘strComputer’ to your own Computer or Server name.
3. Change the user account ‘Administrator’ to your own user credential and give valid password.
4. Save the file with a .vbs extension, for example: ListNetworkSharesAsDiffUser.vbs
5. Run usage in CMD:

C:> CScript <vbscript file path>

Example: CScript C:ScriptsListNetworkSharesAsDiffUser.vbs 
    Run this command from Command Prompt to get list of Network Shares with VBScript using WMI Service by using different User Credential.

Note: You can not use User Credentials for local computer to connect WMI Service.

Click to get VBScript source code as a file Download ListNetworkSharesAsDiffUser.vbs

' ListNetworkSharesbyDiffUser.vbs
' Sample VBScript by WMI to find and list Network Shares.
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------

strComputer = "myComputer"
' you can change your own Computer or Server.

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService= objSWbemLocator.ConnectServer(strComputer , "rootcimv2","Administrator","YourPassword")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

For each objShare in colShares

    Wscript.Echo "Name: " & objShare.Name  & vbCrLf _
                       & "Path: " & objShare.Path & vbCrLf  _ 
                       & "Type: " & objShare.Type  & vbCrLf _
                       & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
                       & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
                       & "Caption: " & objShare.Caption   
Wscript.Echo "---------------------------------"
Next

' Wscript.Echo "Network Share Folders in Remote Computer listed successfully"

WScript.Quit

VBScript to Get List of Network Shares by Dynamic Inputs

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: ListNetworkSharesbyDynamicComputer.vbs
3. Run usage in CMD:

C:> CScript <vbscript file path>

Example: CScript C:ScriptsListNetworkSharesbyDynamicComputer.vbs 
   Run this command in Command prompt to List Network Share Folders with VBScript using WMI Service.
4. Then give the Computer name in the opened Computer name text box and click OK to proceed…

Click to get VBScript source code as a file Download ListNetworkSharesbyDynamicComputer.vbs

' ListNetworkSharesbyDynamicComputer.vbs
' Sample VBScript by WMI to find and list Network Shares.
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------
strComputer = "."

Do
   strComputer = InputBox ("Please enter computer name")
   If strComputer = "" then
      Msgbox "No computer name entered"
   end if
Loop Until strComputer <> ""

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService= objSWbemLocator.ConnectServer(strComputer , "rootcimv2")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

For each objShare in colShares

    Wscript.Echo "Name: " & objShare.Name  & vbCrLf _
                       & "Path: " & objShare.Path & vbCrLf  _ 
                       & "Type: " & objShare.Type  & vbCrLf _
                       & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
                       & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
                       & "Caption: " & objShare.Caption  
Wscript.Echo "---------------------------------" 
Next

' Wscript.Echo "Network Share Folders in Remote Computer listed successfully"

WScript.Quit

Advertisement

2 thoughts on “VBScript to Get List of Network Shares using WMI Service”

  1. Hi.. when I tried too call objShare.Path I m getting an error
    "Object doesn't support this property or method: 'objService.Path'" can you help me out

    Reply
  2. Hi
    Thanks for sharing this info .I am a newbie to WMI and i need ur help to get the PC details using its sjared path. EG:\Pc123dtest is the shared path. I have to get details like C drive free space availablity and network info of "Pc123".Could you please guide me on this
    All help are appreciated

    Thanks again

    Reply

Leave a Comment