#PSTip Check if a folder is shared

Note: This tip requires PowerShell 3.0 or above.

The WMI class Win32_Share gives us the information about all network shares that exist on the local machine. Now, how do we check if a given folder is shared or not?

We can use the Win32_Share WMI class and query for a specific path! I have put this simple logic in a function.

Function IsFolderShared {
        param (

    Process {
       foreach ($Folder in $Path) {
           $WMIFolderPath = $Folder -replace '\\','\\'
           [PSCustomObject] @{
               "FolderPath" = $Folder
               "IsShared" = if (Get-CimInstance -Query "SELECT * FROM Win32_Share WHERE Path='$WMIFolderPath'") { $true } else { $false }


Filed in: Columns, Tips and Tricks Tags: , , , ,
© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: