Network Share Usage Report Script

This PowerShell script generates a report of network share usage by enumerating shared folders and their sizes on a specified server. The script is beneficial for system administrators to monitor shared resources and manage storage effectively.
Step 1: Define Server and Share Path
First, we will set the target server and the share paths we want to investigate. This will allow the script to focus on the relevant resources.

# Define the target server and share path to analyze
$serverName = "YourServerName"  # Replace with your server name
$sharePath = "\\$serverName\*"  # Wildcard to search all shares

In this block, we define the `$serverName` variable for the target server and construct the `$sharePath` using a UNC path. You should replace `”YourServerName”` with your actual server name.
Step 2: Get List of Shared Folders
Next, we will collect the list of shared folders on the defined server using the `Get-WmiObject` cmdlet, which allows us to query WMI classes.

# Retrieve shared folders from the server
$shares = Get-WmiObject -Class Win32_Share -ComputerName $serverName | Where-Object { $_.Path }
$sharesList = $shares | Select-Object Name, Path, Description

This step queries the `Win32_Share` WMI class to get all shares on the specified server. We filter the results to include only those with a valid path and select relevant information such as `Name`, `Path`, and `Description`.
Step 3: Calculate Folder Sizes
For each shared folder, we will now calculate its total size. This is done by summing the sizes of all files within the shared folder.

$folderSizes = @()
foreach ($share in $sharesList) {
    $totalSize = (Get-ChildItem -Path $share.Path -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
    $folderSizes += [PSCustomObject]@{
        Name        = $share.Name
        Path        = $share.Path
        SizeInMB    = [math]::round($totalSize / 1MB, 2)
        Description = $share.Description
    }
}

In this section, we iterate through the list of shared folders. For each share, we use `Get-ChildItem` with recursion to calculate the total size of all files. The results are stored in a custom PowerShell object containing the share’s name, path, size in MB, and description.
Step 4: Output the Report
With the collected data, we will create a clear and concise report to display the information about each shared folder, including its size.

# Display the folder sizes in a formatted table
$folderSizes | Format-Table -AutoSize

This final step outputs the list of shared folders and their sizes in a formatted table for easy reading. `Format-Table -AutoSize` ensures that the columns are properly aligned.
Step 5: Optional – Export the Report to CSV
If needed, we can also export the report to a CSV file for further analysis or record-keeping.

# Export folder sizes report to a CSV file
$exportPath = "C:\NetworkShareUsageReport.csv"
$folderSizes | Export-Csv -Path $exportPath -NoTypeInformation
Write-Host "Report exported to $exportPath"

In this optional step, we export the collected data to a CSV file, allowing for further analysis or sharing with other team members. The `-NoTypeInformation` parameter ensures that no additional type information is included in the CSV file.
By employing this script, system administrators can efficiently monitor network share usage, manage storage, and optimize resource allocation across the network, ensuring a well-maintained environment.