PowerShell Scripting – WmiObject Win32_Share

PowerShell Scripting – WmiObject Win32_Share

The purpose of this script is to list shares on a server.  In fact, this code will enumerate the shares on any computer.

WMI and PowerShell Topics

 ♣

Preliminary Task: Get-WmiObject

I admit that there is a danger of getting side tracked when we start experimenting with these command.  My justification is that research WMI properties is useful in other contexts.

# Research WMI Classes
Get-WmiObject -List
or even
Get-WmiObject -List | Where {$_.name -Like "win32*"}

Note 1: See how the production line of classes is controlled by the pipe | in the middle.

Note 2: Remember that ‘Where’ {takes curly brackets}. 

Note 3: Observe that ‘like’ is a parameter or switch, thus needs a minus sign: -Like.

Five Examples of WmiObject Win32_share

1) Initial Investigation

To display a list of all the objects properties, never miss an opportunity to research properties with Get-Member

Clear-Host
Get-WmiObject Win32_share | Get-Member

2) Basic

Clear-Host
Get-WmiObject Win32_share

3) Switch to a named server

Clear-Host
Get-WmiObject Win32_share -computer YourBigServer

Note 4: Naturally, change the name of YourBigServer to the name of a machine on your subnet.

4) Filter Properties Path, then Name, but NO Description

Clear-Host
Get-WmiObject Win32_share | FT path, name -autosize

Note 5: FT is an alias of Format-Table.  Other options are format-List or format-custom.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

5) Display Shares, which are not hidden ($)

Clear-Host
WmiObject Win32_share | Where {$_.name -NotLike "*$"} | `
Sort-Object -property path | FT path, name -autosize

Note 6: Strictly speaking the term is Where-Object, however, I removed the redundant noun -object.  You could also try omitting ‘-object’ from ‘Sort-Object

Challenge: Try: Where {$_.name -Like "*$"}

How to create a share with PowerShell »

Summary of Scripting PowerShell Shares

Firstly, remember that Win32_share is a WmiObject.  Secondly, once you have mastered the basics you may wish to filter the shares by using a ‘Where’ statement.  Finally, you may wish to modify the number and position of the properties displayed in the output.

If you like this page then please share it with your friends

 


See more PowerShell share examples including WMI

PowerShell WMI   • Create Win32_Share   • WMI Shares   • Free Permissions Analyzer Tool

Get-Acl  • PowerShell Share Error Codes   • Win32_ComputerSystem  • PowerShell 3.0 CIM

Windows PowerShell  • Free WMI Monitor  • Cacls   • Query   • PowerShell Printer Scripts

Please email me if you have a example scripts.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.