Hi I am still fairly new to powershell. I was asked about getting a list of all servers that have shares on the network. I came across a nice script that does this, but I would also like it to show the permissions of each share as well if possible and have it exported out to a csv or html. Any help is much appreciated.

Here is the script that I have.

# 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.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$”))  { 
 
            # Output to screen the name and path of the shared folder 
            write-host $share.name $share.path; 
            }
 
        }
 
    write-host "";
 
    }
}
 
7 Spice ups

You can use Get-ACL to list the permissions.

FYI, Write-Host is discouraged, because it de-serializes objects to output them in the console, which breaks the pipeline. Here is a post from Jeffrey Snover, lead developer of the original Powershell team, that explains the issue.

1 Spice up

The article Evan posted is helpful. Using Write-Host won’t let you export to CSV or HTML. Change Write-Host to Write-Output, and you can continue to pipe the results (eventually to a CSV). You can use Export-CSV to do just that.