Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Check for logged on Users
- if ((Get-WmiObject -Class Win32_ComputerSystem | select UserName).UserName.length -eq 0){
- #log users off
- }
- #Set Accounts to Preserve
- $PreservedAccounts = "Administrator"
- #Get a list of RegPaths
- $Profiles = Get-ChildItem -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList" | ?{ $_.Name -match "S-1-5-21"}
- #Go through Each profile path
- foreach($p in $Profiles){
- #get the profile path
- $PIP = (Get-ItemProperty -Path $p.PSPath).ProfileImagePath
- Write-Host "Profile Image Path: $PIP"
- #Get UserName from path
- [Void]($PIP -match "[^\\]+\\([^\\]+$)")
- $UserName = $Matches[1]
- if ($UserName -match $PreservedAccounts){
- Write-Host "Skipping $UserName"
- } else{
- Write-Host "Removing $UserName"
- #Remove Folder and items
- Remove-Item -Path $PIP -Recurse -Force -Verbose
- #Remove Registry Folder
- Remove-Item -Path $p.PSPath -Force -Recurse -Verbose
- }
- }
- #Get Session info
- $sessions = quser | Select-Object -Skip 1 | ForEach-Object {
- $CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
- $HashProps = @{
- UserName = $CurrentLine[0].Replace(">","")
- }
- # If session is disconnected different fields will be selected
- if ($CurrentLine[2] -eq 'Disc') {
- $HashProps.SessionName = $null
- $HashProps.Id = $CurrentLine[1]
- $HashProps.State = $CurrentLine[2]
- $HashProps.IdleTime = $CurrentLine[3]
- $HashProps.LogonTime = $CurrentLine[4..6] -join ' '
- $HashProps.LogonTime = $CurrentLine[4..($CurrentLine.GetUpperBound(0))] -join ' '
- } else {
- $HashProps.SessionName = $CurrentLine[1]
- $HashProps.Id = $CurrentLine[2]
- $HashProps.State = $CurrentLine[3]
- $HashProps.IdleTime = $CurrentLine[4]
- $HashProps.LogonTime = $CurrentLine[5..($CurrentLine.GetUpperBound(0))] -join ' '
- }
- New-Object -TypeName PSCustomObject -Property $HashProps |
- Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement