- 1 year ago
PowerShell 2 - Being Able To Get All Share Folders And People Shared With
In PowerShell 2.0, you can use the net share
command to retrieve a list of shared folders on a Windows system. However, retrieving the list of people shared with each folder may not be directly available using native PowerShell 2.0 commands. You might need to resort to using the cacls
command or parsing the output of net share
to gather the information.
Here's an example that demonstrates how to retrieve the shared folders using net share
:
powershell$sharedFolders = net share | Select-String -Pattern "Disk|Print" | ForEach-Object {
$folderInfo = $_ -split "\s+"
$folderName = $folderInfo[0]
[PSCustomObject]@{
Name = $folderName
}
}
$sharedFolders
This code uses the net share
command to retrieve all shared folders on the system and filters the output to include only shared disks and printers. It then creates custom objects to represent each shared folder with the Name
property.
To retrieve the list of people shared with each folder, you can try using the cacls
command in conjunction with the shared folder names obtained earlier:
powershell$sharedFolders | ForEach-Object {
$folderName = $_.Name
# Retrieve ACL information using cacls command
$aclInfo = cacls $folderName
# Extract the list of people shared with the folder
$sharedWith = $aclInfo | Select-String -Pattern "\S+:(.*)"
# Update the shared folder object with the sharedWith property
$_ | Add-Member -NotePropertyName SharedWith -NotePropertyValue ($sharedWith | ForEach-Object { $_.Matches.Groups[1].Value })
$_
}
This code uses the cacls
command to retrieve the access control list (ACL) information for each shared folder. It then extracts the list of people shared with the folder using regex pattern matching. Finally, it adds a SharedWith
property to each shared folder object with the list of people shared with the folder.
Please note that the cacls
command and its output format can vary across different versions of Windows. You may need to adjust the parsing logic accordingly to match the output of cacls
on your system.
Keep in mind that PowerShell 2.0 is quite old and lacks some features and improvements found in later versions. It is recommended to use a more recent version of PowerShell if possible.