Script to Generate a Comprehensive List of Network Shares

1 min. readlast update: 09.18.2024

The `Get-SharesList.ps1` script is designed to retrieve a list of shared resources, such as file shares, from a specified environment. It collects information about the shared locations and outputs relevant data, typically used for monitoring or auditing purposes. This script is useful in scenarios where an administrator needs to analyze or document the shared file systems within a network for security or management purposes.

Here is the script:

# Specify the server name
$server = "\\zagarnas"

# Get the list of shares
$shares = (net view $server | Where-Object { $_ -match 'Disk' } | ForEach-Object { $_.Split(' ')[0].Trim() })

# Create an array to hold the results
$results = @()

# Loop through each share
foreach ($share in $shares) {
    # Get the share path
    $sharePath = $server + "\" + $share

    # Check if the share is accessible
    if (Test-Path $sharePath) {
        # Get the list of folders in the share
        $results += Get-ChildItem -Path $sharePath -Directory | select Name,FullName,LastAccessTime,LastWriteTime
    }
}

# Export the results to a CSV file
$results | Export-Csv -Path "SharesAndFolders.csv" -NoTypeInformation

Was this article helpful?