Jump to content

Recommended Posts

Posted (edited)

Hi all,

 

Someone recently contacted me RE Delprof and I shared some alternative scripts with them, however I wanted to merge these scripts together;

 

One works as it should, deletes all profiles - (well, except some weird ones for whatever reason, but 99%)

 

$profiles = $null

 

$profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser"))}

 

if ($profiles -ne $null) {

$profiles | Remove-WmiObject

}

 

Anyway, the second script sort of works, but misses even more profiles, BUT it has the time stamp for selective profiles

 

Get-CimInstance win32_userprofile -verbose | Where {$_.LastUseTime -lt $(Get-Date).Date.AddDays(-14)} | Remove-CimInstance -Verbose

 

So I thought I would combine them, but it doesn't work - anyone have any ideas?

 

$profiles = $null

 

$profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser") -and $_.LastUseTime -lt $(Get-Date).Date.AddDays(-14))}

 

if ($profiles -ne $null) {

$profiles | Remove-WmiObject

}

Edited by Warwick_Tech
Posted
(Sorry, I might have typed too quick - I was thinking that was the equivalent of -properties * that is sometimes used to get extra properties, so my above comment may be pointless)
Posted
(Sorry, I might have typed too quick - I was thinking that was the equivalent of -properties * that is sometimes used to get extra properties, so my above comment may be pointless)

 

No worries, I did try with the -verbose switches, but tbh I'm not sure what they did anyway. I have the feeling it is working as it just goes back to the > prompt, but I think the switch for adding days it's what's causing the issue.

Posted
how does it not work? $profiles is null or "$profiles | Remove-WmiObject" doesn't work?

 

Good question, it just quickly returns to the > without showing an error, but it doesn't work as no profiles are removed, even ones over the 14 day limit; remove the -14 days property and it will chug through and delete everything...

 

Silly question is there a switch to show the output of the script so I can get more detailed info?

Posted (edited)
Good question, it just quickly returns to the > without showing an error, but it doesn't work as no profiles are removed, even ones over the 14 day limit; remove the -14 days property and it will chug through and delete everything...

 

Silly question is there a switch to show the output of the script so I can get more detailed info?

 

I suggest print $profiles before trying to delete anything to check what is in that variable.

 

also this looks to contain relevant information: https://stackoverflow.com/questions/70048684/remove-wmiobject-delete-user-profiles

Edited by browolf
  • Thanks 1
Posted

I haven't tested this, but there are some brackets missing on the $_.Lastusetime you combined into the Where. Does this work any better?

 

$profiles = $null

$profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser") -and ($_.LastUseTime -lt $(Get-Date).Date.AddDays(-14)))}

if ($profiles -ne $null) {
   $profiles | Remove-WmiObject
}

  • Thanks 1
Posted
I haven't tested this, but there are some brackets missing on the $_.Lastusetime you combined into the Where. Does this work any better?

 

$profiles = $null

$profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser") -and ($_.LastUseTime -lt $(Get-Date).Date.AddDays(-14)))}

if ($profiles -ne $null) {
   $profiles | Remove-WmiObject
}

 

Still no joy :(

 

- - - Updated - - -

 

I suggest print $profiles before trying to delete anything to check what is in that variable.

 

also this looks to contain relevant information: https://stackoverflow.com/questions/70048684/remove-wmiobject-delete-user-profiles

 

Just running print $profiles returns:

No file to print

 

Though that's the same on my PC...

Posted
The $_.lastusetime is looking for profile used in the last 2 weeks, is this what you're looking for? Or are you looking for profiles that haven't been used in the last 2 weeks?
Posted
The $_.lastusetime is looking for profile used in the last 2 weeks, is this what you're looking for? Or are you looking for profiles that haven't been used in the last 2 weeks?

 

Oh - Yes it should be profiles NOT used in the last 2 weeks

 

but to be fair, it's not even removing those

Posted (edited)
I may have been wrong :D acting weird on my pc.... Quick edit, looks like it was my PC playing up! -lt is the correct operator Edited by ThomL
Posted

Does this output the correct list of profiles?

 

$profiles = $null
$delDate = (Get-Date).AddDays(-14)
$persistentProfilePaths = "C:\Users\Administrator","C:\Users\UpdatusUser"

$profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LastUseTime -lt $delDate))}

foreach($profile in $profiles | where {$persistentProfilePaths -notcontains $_.LocalPath}) {
   write-host "$($profile.LastUseTime) $($profile.LocalPath)"
   #$profiles | Remove-WmiObject
}

Posted
Does this output the correct list of profiles?

 

$profiles = $null
$delDate = (Get-Date).AddDays(-14)
$persistentProfilePaths = "C:\Users\Administrator","C:\Users\UpdatusUser"

$profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LastUseTime -lt $delDate))}

foreach($profile in $profiles | where {$persistentProfilePaths -notcontains $_.LocalPath}) {
   write-host "$($profile.LastUseTime) $($profile.LocalPath)"
   #$profiles | Remove-WmiObject
}

 

No, just takes me back to >

 

I think there's more going on here, as I can't seem to get it to recognise these are even profiles...

 

what makes less sense, is as I say the base wipes all of them...

$profiles = $null

 

$profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser"))}

 

if ($profiles -ne $null) {

$profiles | Remove-WmiObject

}

 

Exit

Capture.JPG

Posted

I suggest using windows terminal as it's a lot nicer than the built in ide. With terminal you can run ps commands on the command line and it's colour coded. When you're in this situation it's helpful to deconstruct the script as much as possible to pinpoint the problem. On the commandline you can just type a variable name to get it's contents e.g

PS D:\>$profiles

 

 

https://apps.microsoft.com/store/detail/windows-terminal/9N0DX20HK701?hl=en-gb&gl=

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...