Home > Mobile >  Creating an Array from get-childitem folder paths, to delete windows user profiles
Creating an Array from get-childitem folder paths, to delete windows user profiles

Time:02-02

I'm writing a script to delete Windows user profiles older than 90 days.

When running the following command

Get-ciminstance -Class win32_userprofile | where-object {$_.LastuseTime -gt (get-date).AddDays(90)

The "LastUseTime" for my workstations are all resetting to the every time the machine was rebooted, which are automatically rebooted every 2 weeks. So in return I have all profiles returned when running that cmdlet.

My attempt at resolving this is going into the C:\users\ directory and identifying the user folders that were last modified over 90 days ago with the following script and assigning the values to a variable. Users have IDs of 6 digits (ex. 123456, 111222.e, 021561). We want to avoid deleting profiles that have letters in the names (ex. c:\users\markdavis c:\users\administrator)

$profiles = Get-childitem -Path C:\Users\* -Directory | where-object {$_.LastWriteTime -lt (get-date).AddDays(-90)} | Where-Object {$_.Name -match "\d\d\d\d\d\d"} | Select-Object -Property FullName

checking the variable will return

PS C:\WINDOWS\system32> $profiles
FullName         
--------         
C:\Users\123456
C:\Users\111222.e
C:\Users\021561  

Now here is where I am having trouble completing the script as the following cmdlet is failing to pull up the correctly filtered user profiles before I use Remove-CIMInstance to delete them, I tried many different ways to try to make it work, and now questioning if it is even possible to pass the variable to the following script. Appreciate the help!

$profiles = Get-childitem -Path C:\Users\* -Directory | where-object {$_.LastWriteTime -lt (get-date).AddDays(-90)} | Where-Object {$_.Name -match "\d\d\d\d\d\d"} | Select-Object -Property FullName

forEach($profile in $profiles){
Get-ciminstance -Class win32_userprofile | Where-object {$_.LocalPath -match "$profile"} | Remove-CimInstance
}

I've also tried passing forcing into an array - but was also unsuccessful.

$profiles = @(Get-childitem -Path C:\Users\* -Directory | where-object {$_.LastWriteTime -lt (get-date).AddDays(-90)} | Where-Object {$_.Name -match "\d\d\d\d\d\d"} | Select-Object -Property FullName)

CodePudding user response:

Try (getting the FullName properties as array):

$profiles = (Get-ChildItem -Path C:\Users\* -Directory | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-90)} | Where-Object {$_.Name -match "\d\d\d\d\d\d"}).FullName

forEach($profile in $profiles){
    Get-CimInstance -class win32_userprofile | Where-Object {$_.LocalPath -match "$profile"} | Remove-CimInstance
}

Or (using the FullName property of each object we got using Where-Object):

$profiles = Get-ChildItem -Path C:\Users\* -Directory | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-90)} | Where-Object {$_.Name -match "\d\d\d\d\d\d"}

forEach($profile in $profiles){
    Get-ciminstance -Class win32_userprofile | Where-object {$_.LocalPath -match $profile.FullName} | Remove-CimInstance
}
  • Related