Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Check for logged on Users
  2.  
  3. if ((Get-WmiObject -Class Win32_ComputerSystem | select UserName).UserName.length -eq 0){
  4.     #log users off
  5. }
  6.  
  7. #Set Accounts to Preserve
  8. $PreservedAccounts = "Administrator"
  9.  
  10.  
  11. #Get a list of RegPaths
  12. $Profiles = Get-ChildItem -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList" | ?{ $_.Name -match "S-1-5-21"}
  13.  
  14.  
  15. #Go through Each profile path
  16. foreach($p in $Profiles){
  17.    
  18.     #get the profile path
  19.     $PIP =  (Get-ItemProperty -Path $p.PSPath).ProfileImagePath
  20.     Write-Host "Profile Image Path: $PIP"
  21.    
  22.     #Get UserName from path
  23.     [Void]($PIP -match "[^\\]+\\([^\\]+$)")
  24.     $UserName = $Matches[1]
  25.     if ($UserName -match $PreservedAccounts){
  26.         Write-Host "Skipping $UserName"
  27.     } else{
  28.         Write-Host "Removing $UserName"
  29.         #Remove Folder and items
  30.         Remove-Item -Path $PIP -Recurse -Force -Verbose
  31.         #Remove Registry Folder
  32.         Remove-Item -Path $p.PSPath -Force -Recurse -Verbose
  33.     }
  34.  }
  35.  
  36.  
  37.  #Get Session info
  38.  
  39. $sessions = quser | Select-Object -Skip 1 | ForEach-Object {
  40.                 $CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
  41.                $HashProps = @{
  42.                     UserName = $CurrentLine[0].Replace(">","")
  43.                 }
  44.  
  45.                 # If session is disconnected different fields will be selected
  46.                 if ($CurrentLine[2] -eq 'Disc') {
  47.                        $HashProps.SessionName = $null
  48.                         $HashProps.Id = $CurrentLine[1]
  49.                         $HashProps.State = $CurrentLine[2]
  50.                         $HashProps.IdleTime = $CurrentLine[3]
  51.                         $HashProps.LogonTime = $CurrentLine[4..6] -join ' '
  52.                        $HashProps.LogonTime = $CurrentLine[4..($CurrentLine.GetUpperBound(0))] -join ' '
  53.                } else {
  54.                         $HashProps.SessionName = $CurrentLine[1]
  55.                         $HashProps.Id = $CurrentLine[2]
  56.                         $HashProps.State = $CurrentLine[3]
  57.                         $HashProps.IdleTime = $CurrentLine[4]
  58.                         $HashProps.LogonTime = $CurrentLine[5..($CurrentLine.GetUpperBound(0))] -join ' '
  59.                }
  60.  
  61.                 New-Object -TypeName PSCustomObject -Property $HashProps |
  62.                 Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
  63.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement