hello I ran the script from admin Powershell ISE , but nothing happened. what am I do wrong?
I'm trying to run the script but receiving the following error, please advise: Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime." At C:\Users\administrator\Desktop \Cleanup-UserPr ofiles.ps1:26 char:5 + $dateLastUsed = [datetime]::Par seExact(($profi le.lastusetime -rep ... + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocatio nException + FullyQualifiedE rrorId : FormatException Exception calling "Delete" with "0" argument(s): "" At C:\Users\admini strator\Desktop \Cleanup-UserPr ofiles.ps1:48 char:9 + $profile.Delete () + ~~~~~~~~~~~~~~~ ~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocatio nException + FullyQualifiedE rrorId : DotNetMethodExc eption Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime." At C:\Users\admini strator\Desktop \Cleanup-UserPr ofiles.ps1:26 char:5 + $dateLastUsed = [datetime]::Par seExact(($profi le.lastusetime -rep ... + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocatio nException + FullyQualifiedE rrorId : FormatException
Steve, the error is coming from user accounts with no LastUseTime value, you can use the following to see which accounts are causing issues, most likely default accounts that exist but have not logged in: Get-WmiObject -Class Win32_UserProfile | Where{$_.LastUs eTime -eq $Null} | Select LocalPath, LastUseTime | Sort-Object LocalPath This issue doesn't appear to cause problems as the "$DateLastused -ge $DateLimit" check fails but it can cause concern seeing a load of red flying by. Possible way to get around this, if the LastUseTime is Null then set the $DateLastUsed value to be today's date/time so there is always a valid value and the profile won't be deleted. So wrap this code in an If/Else: #Check if profile is in date range $dateLastUsed = [datetime]::Par seExact(($profi le.lastusetime -replace '\..+$',''),'yy yyMMddHHmmss',$ null )