Monitor folders

When you want to monitor shares on a server and find out who is using those shares I found a nice script here

$computer = "LocalHost"
$namespace = "root\CIMV2"
$userSessions = Get-WmiObject -class Win32_ServerConnection -computername $computer -namespace $namespace

if($userSessions -ne $null)
    Write-Host "The following users are connected to your PC: "

    foreach ($userSession in $userSessions)
        $userDetails = [string]::Format("User {0} from machine {1} on share: {2}", $userSession.UserName, $userSession.ComputerName, $userSession.ShareName)
        Write-Host $userDetails


if you have a text file with foldernames and you want to recurse through them and delete them you could use the following script:
cd G:\Home
$shares = get-content c:\temp\homeshare.txt
$shares | foreach {if (test-path $_) {Remove-Item -Path $_ -Recurse -force

If you are looking for folders that have orphan SID in the access of those folders you can weed them out like this:
Get-ChildItem g:\home -recurse | Where {$_.PSISContainer} | get-acl | Where {$_.AccessToString -match "S-1-
5*"} | Select @{n='Folder';E={($_.pspath).split("::")[3]}},owner,accessToString | export-csv c:\temp\unresolvedSIDs.csv

If you have a lot of txt files in different folders in a particular folder you can search for them and move them like this:
$drv = Get-ChildItem F:\txtfolder -recurse -filter "*.txt" | %{$_.DirectoryName} | get-unique | foreach {
if ($_.ToString() -eq "F:\donotmove"){
     Write-host "Skipping $_"   
else {Move-item $_.ToString() -Destination "Z:\txt"
Write-host $_


on me
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License