This script will, unaltered, find all servers in Active Directory and probe each for its shared folders (using WMI). It is set to automatically exclude the default shares lik SYSVOL, system drive shares (C$, D$ etc), printer shares and others.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# Add powershell modules for Active Directory import-module activedirectory; # Get list of servers from Active Directory $servers = get-adcomputer -filter {operatingsystem -like "*server*"} | where {$_.enabled -eq $true}; # Loop through each server found foreach ($server in $servers) { # Check if server can be pinged if(test-connection -cn $server.name -quiet -count 1) { # Output to screen the name of the server write-host "-------------"; write-host $server.name; write-host "-------------"; # Get the list of shared folders $shares = gwmi win32_share -computer $server.name; # Loop through each shared folder found foreach ($share in $shares) { # Exclude default drive shares, other system default shares and printers if (($share.name -ne "print$") -and ($share.path -notlike "*LocalsplOnly*") -and ($share.name -notmatch ".\$") -and ($share.name -ne "ipc$") -and ($share.name -ne "sysvol") -and ($share.name -ne "netlogon")-and ($share.name -ne "admin$")) { # Output to screen the name and path of the shared folder write-host $share.name $share.path; } } write-host ""; } } |
The result should list each server name, with a list of shares names and local drive locations of those shares:
I changed ($share.name -notmatch “.\$”) to ($share.name -notmatch “^.\$”) because it was matching on all hidden shares instead of just the admin drive shares.
Also, there’s another print share: ($share.name -ne “prnproc$”)
Final statement:
if (($share.name -ne “print$”) -and ($share.name -ne “prnproc$”) -and ($share.path -notlike “*LocalsplOnly*”) -and ($share.name -notmatch “^.\$”) -and ($share.name -ne “ipc$”) -and ($share.name -ne “sysvol”) -and ($share.name -ne “netlogon”)-and ($share.name -ne “admin$”))
Good catch.
That is great. I have been working on getting this to export to CSV or HTML I don’t think I know what I need to know yet.
Can anyone help?
Rather than using write-host, you should build a comma separated string like this:
$out = $server.name + "," + $share.name + "," + $share.path
And then append that to a file with:
$out >> c:\temp\output.csv
Thank you KAMAL,
You are so professional! Your script give me a big favor!!
Can you output the total, free, and used space of each share to file also?
Yes – you definitely can, although it’s a little more complex.
After you run the gwmi win32_share command, it returns the Path to the share (as $share.path in the foreach loop). You can use that drive letter in a gwmi win32_logicaldisk query to get the total/used/free space, similar to how I used it here:
https://hkeylocalmachine.com/?p=745