Get user mapped drives
Today we bring you a script to help record users mapped drives. In this scenario, the script was deployed as a logon script and the report was stored on a shared UNC path for collection by administrators.
This is a simple demonstration of how to easily capture WMI information and export to CSV for cataloguing. It also gives a good example of using get-date to generate dynamic file names based on execution time.
# Construct an object to store our results
$Report = @()
# Capture the username from the local variables
$user = $env:USERNAME
# Set our filename based on the execution time
$filenamestring = "$user-$(get-date -UFormat "%y-%b-%a-%H%M").csv"
# Via WMI report the mapped drives on localhost
$colDrives = Get-WmiObject Win32_MappedLogicalDisk -ComputerName localhost
foreach ($objDrive in $colDrives) {
# For each mapped drive - build a hash containing information
$hash = @{
Username = $user
MappedLocation = $objDrive.ProviderName
DriveLetter = $objDrive.DeviceId
}
# Add the hash to a new object
$objDriveInfo = new-object PSObject -Property $hash
# Store our new object within the report array
$Report += $objDriveInfo
}
# Export our report array to CSV and store as our dynamic file name
$Report | Export-Csv -NoType $filenamestring
Get user mapped drives.
