{"id":2861,"date":"2013-09-16T00:01:00","date_gmt":"2013-09-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/09\/16\/conditional-user-profile-deletion-revisited\/"},"modified":"2013-09-16T00:01:00","modified_gmt":"2013-09-16T00:01:00","slug":"conditional-user-profile-deletion-revisited","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/conditional-user-profile-deletion-revisited\/","title":{"rendered":"Conditional User Profile Deletion Revisited"},"content":{"rendered":"<p><strong>Summary<\/strong>: Guest blogger, Bob Stevens, revises his script for conditional user profile removal.\nMicrosoft Scripting Guy, Ed Wilson, is here. I am happy to welcome back guest blogger, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/bob+stevens\/\" target=\"_blank\">Bob Stevens<\/a>. Take it away Bob&hellip;\nPreviously I released a blog post entitled <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/06\/09\/weekend-scripter-use-powershell-for-conditional-user-profile-removal.aspx\" target=\"_blank\">Weekend Scripter: Use PowerShell for Conditional User Profile Removal<\/a>. If you have not read this post, please do because it is necessary so that you&rsquo;ll understand what I am talking about today.\nIt was pointed out to me that Windows Vista and later operating systems create temporary user directories instead of re-creating the directories when the user next signs in. To start at a reference point, here is the original script that I created:<\/p>\n<p style=\"padding-left: 30px\">IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;) {<\/p>\n<p style=\"padding-left: 30px\">$over30dayprofiles = @(Get-ChildItem -force &#8220;C:Documents and Settings&#8221; | Where {<\/p>\n<p style=\"padding-left: 30px\">((Get-Date)-$_.lastwritetime).days -ge 30<\/p>\n<p style=\"padding-left: 30px\">} )<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;) {<\/p>\n<p style=\"padding-left: 30px\">$over30dayprofiles = @(Get-ChildItem -force &#8220;C:Users&#8221; | Where {<\/p>\n<p style=\"padding-left: 30px\">((Get-Date)-$_.lastwritetime).days -ge 30<\/p>\n<p style=\"padding-left: 30px\">} )<\/p>\n<p style=\"padding-left: 30px\">Set-Content &#8220;.over30dayprofiles.txt&#8221; $over30dayprofiles<\/p>\n<p style=\"padding-left: 30px\">$over30dayprofiles = @(Get-Content &#8220;.over30dayprofiles.txt&#8221; | Foreach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;$_ -replace &#8216;Administrator&#8217;, &#8221; `<br \/> &nbsp; &nbsp;-replace &#8216;LocalService&#8217;, &#8221; `<br \/> &nbsp; &nbsp;-replace &#8216;NetworkService&#8217;, &#8221;<\/p>\n<p style=\"padding-left: 30px\">} | Select-String -pattern &#8220;w&#8221; | ForEach-Object { $_.line })<\/p>\n<p style=\"padding-left: 30px\">$count = $over30dayprofiles.count<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">Set-Location &#8216;C:Documents and Settings&#8217;<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Remove-Item -force -recurse $over30dayprofiles[$i]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$i++<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Until($i -ge $count)\nThe first alteration that we need is to add a message to the user in the event that there are no profiles to remove. To do so, directly after we remove the desired accounts and white space from the variable <strong>$over30dayprofiles<\/strong>, we add the following:<\/p>\n<p style=\"padding-left: 30px\">If ($over30dayprofiles.count -eq 0){Write-Host &#8221; all profiles have been accessed in the last 30 days&#8221;}<\/p>\n<p style=\"padding-left: 30px\">If ($over30dayprofiles.count -gt 0){\nBecause of the brace that is located at the end of the second <strong>IF<\/strong> statement, we need to add a closing brace at the end of the script. This will run the command inside the script only when there are profiles that have not been accessed within the past 30 days.\nDirectly after that, we add the command to determine the SID of each profile that we are going to remove. First we need to define in a variable the number of profiles that we are going to remove:<\/p>\n<p style=\"padding-left: 30px\">$count = $over30dayprofiles.count\nThe <strong>Count <\/strong>property simply counts the items in the <strong>$over30dayprofiles<\/strong> array. Next we need to state that this is only to be<em> <\/em>run<em> <\/em>in any version of Windows 6 and later. I explained how to do this in another previous post:<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/06\/08\/weekend-scripter-use-powershell-to-generate-a-recent-profile-report.aspx\" target=\"_blank\"> Weekend Scripter: Use PowerShell for Conditional User Profile Removal<\/a>.<\/p>\n<p style=\"padding-left: 30px\">IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;) {}\nInside the braces, we are going to use the WMI object <strong>Win32_userprofile<\/strong>. To do this, we need to use the <strong>Get-WMIobject<\/strong> cmdlet to call <strong>Win32_userprofile<\/strong>:<\/p>\n<p style=\"padding-left: 30px\">Get-WMIobject win32_userprofile\nBecause we only want two properties (<strong>SID<\/strong> and <strong>Path<\/strong>), we are going to filter it by using the pipe operator (<strong>|<\/strong>) followed by the <strong>Select<\/strong> cmdlet:<\/p>\n<p style=\"padding-left: 30px\">Get-WMIobject win32_userprofile | select\nThe property types are the same for all profiles, so no quotations are needed. We simply need to separate them by a comma and a space:<\/p>\n<p style=\"padding-left: 30px\">Get-WMIobject win32_userprofile | Select SID, Localpath\nWe are going to manipulate this further, so we pipe it into the file named <strong>ProfileInfo.txt<\/strong>:<\/p>\n<p style=\"padding-left: 30px\">Get-WMIobject win32_userprofile | Select SID, Localpath | Out-File .ProfileInfo.txt\nThe next portion of the script utilizes a <strong>Do-Until<\/strong> loop, so we set a counter at <strong>0<\/strong>:<\/p>\n<p style=\"padding-left: 30px\">$i = 0\nNow let&rsquo;s create the item that we are going to be manipulating:<\/p>\n<p style=\"padding-left: 30px\">New-Item -path .SID.txt -itemtype File\nAnd finally, we can start the <strong>Do-Until<\/strong> loop:<\/p>\n<p style=\"padding-left: 30px\">Do{}<\/p>\n<p style=\"padding-left: 30px\">Until()\nInside the braces that follow the <strong>Do<\/strong> statement, we are going to pull the contents of <strong>ProfileInfo.txt<\/strong>, select any string that has the currently selected value from the <strong>$over30dayprofiles<\/strong> array, and split the line at the first whitespace that occurs. This is done by first retrieving the data, and then selecting the line:<\/p>\n<p style=\"padding-left: 30px\">Get-Content .ProfileInfo.txt | Select-String -pattern $over30dayprofiles[$i] |\nNow we split the line at the first white space by using a regular expression:<\/p>\n<p style=\"padding-left: 30px\">Foreach-Object { $_ -split &rsquo;s+&rsquo; }\nAnd we select the first half of the string:<\/p>\n<p style=\"padding-left: 30px\">ForEach-Object { $_ -split &#8216;s+&#8217; | select -f 1}\nIf we append this command to the end of the content and set the output location, this is the result:<\/p>\n<p style=\"padding-left: 30px\">Get-Content .ProfileInfo.txt | Select-String -pattern $over30dayprofiles[$i] | ForEach-Object { $_ -split &#8216;s+&#8217; | select -f 1} | Add-Content .SID.txt\nNow we nest this into the <strong>Do<\/strong> portion of our <strong>Do-Until<\/strong> loop, and add the escape value. An escape value is a predetermined number that will end the loop when the counter meets the inequality presented by the <strong>Until<\/strong> or <strong>While<\/strong> statements.<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">Get-Content .ProfileInfo.txt | Select-String -pattern $over30dayprofiles[$i] | ForEach-Object { $_ -split &#8216;s+&#8217; | select -f 1} | Add-Content .SID.txt<\/p>\n<p style=\"padding-left: 30px\">$i++}<\/p>\n<p style=\"padding-left: 30px\">Until($i -ge $count)\nAs you can see, the escape value is <strong>$i<\/strong> when our counter reaches a value greater than <strong>$count<\/strong>. <strong>$count <\/strong>is the number of elements in the <strong>$over30daysprofiles<\/strong> array.<\/p>\n<p style=\"padding-left: 30px\">IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;) {<\/p>\n<p style=\"padding-left: 30px\">Get-WMIobject win32_userprofile | select SID, Localpath | Out-File .ProfileInfo.txt<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">New-Item -path .SID.txt -itemtype File<\/p>\n<p style=\"padding-left: 30px\">DO {<\/p>\n<p style=\"padding-left: 30px\">get-content .ProfileInfo.txt | Select-String -pattern $over30dayprofiles[$i] | ForEach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $_ -split &#8216;s+&#8217; | select -f 1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } | Add-Content .SID.txt<\/p>\n<p style=\"padding-left: 30px\">$i++<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Until($i -ge $count)<\/p>\n<p style=\"padding-left: 30px\">$SID = @(Get-Content .SID.txt)\nNow we are going to take the contents of the SID.txt file, and set them as the values for another array, <strong>$SID<\/strong>:<\/p>\n<p style=\"padding-left: 30px\">$SID = @(Get-Content .SID.txt)\nBecause this script is to be run on any current computer, we need to add the following line so that we are working out of the Documents and Settings folder:<\/p>\n<p style=\"padding-left: 30px\">IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;) {Set-Location &#8216;C:Documents and Settings&#8217;}\nFor the sake of clarity, the next set of script will be explained in its entirety.\nFirst we set our counter and <strong>Do-Until<\/strong> statement:<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">Do {}<\/p>\n<p style=\"padding-left: 30px\">Until()\nNow we add the conditional <strong>IF<\/strong> statements:<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF(){}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF(){}<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Until()\nNext we add our operating system identifiers by using the WMI object we have already been using:<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;){}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;){}<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Until()\nHere is where the script changes depending on the operating system. For version 5 operating systems, we need to simply remove the profile folders because they will re-create if the user signs in again. It was suggested that I use the utility <strong>Delprof<\/strong> for this, but because there is no way to differentiate between administrator accounts and user accounts, we will go with this:<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;){<\/p>\n<p style=\"padding-left: 30px\"><strong>Remove-Item -force -recurse $over30dayprofiles[$i]<\/strong>}<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;){}<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Until()\nNow we move on to what actually changed. As expressed in the comments from the previous post, when you delete the profile folder from the <strong>Users<\/strong> folder in Windows Vista and later, the operating system will continually generate a temporary profile because the registry key located at:<\/p>\n<p style=\"padding-left: 30px\">&ldquo;HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileList&rdquo;\nSo instead of removing the profile folder or doing some truly interesting stuff with the registry, we are going to use the <strong>win32_userprofile<\/strong> WMI object, this bit of script is courtesy of the user, JRV. Specifically, we are going to filter it for the <strong>SID <\/strong>in the <strong>$SID<\/strong> array that corresponds with the counter, and we are going to set this as the value of the <strong>$profile<\/strong> variable:<\/p>\n<p style=\"padding-left: 30px\">$profile = Get-WmiObject win32_userprofile -filter &#8220;SID=&#8217;$($SID[$i])'&#8221;\nNow that we have the <strong>$profile<\/strong> variable defined with the desired <strong>SID<\/strong>, we are going to delete the profile that it refers to. This is done with the <strong>Delete() <\/strong>property (this will NOT work without the empty parentheses).<\/p>\n<p style=\"padding-left: 30px\">$profile.delete()\nNow we add these two lines together and nest them in our second <strong>IF<\/strong> statement:<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;){Remove-Item -force -recurse $over30dayprofiles[$i]}<\/p>\n<p style=\"padding-left: 30px\">IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;){ <strong>$profile = Get-WmiObject win32_userprofile -filter &#8220;SID=&#8217;$($SID[$i])'&#8221;<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>&nbsp;<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>$profile.delete()<\/strong>}<strong><\/strong><\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Until()\nWe need to add the rest of the script to increment the counter:<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;){Remove-Item -force -recurse $over30dayprofiles[$i]}<\/p>\n<p style=\"padding-left: 30px\">IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;){ <strong>$profile = Get-WmiObject win32_userprofile -filter &#8220;SID=&#8217;$($SID[$i])'&#8221;<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>&nbsp;<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>$profile.delete()<\/strong>}<strong><\/strong><\/p>\n<p style=\"padding-left: 30px\">&nbsp;$i++<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Until($i -ge $count)\nAnd finally, we need to close the brace that we left open with <strong>If &ldquo;($over30dayprofiles.count -gt 0){&ldquo;<\/strong> and clean up our temporary files:<\/p>\n<p style=\"padding-left: 30px\">Remove-Item .over30dayprofiles.txt<\/p>\n<p style=\"padding-left: 30px\">Remove-Item .ProfileInfo.txt<\/p>\n<p style=\"padding-left: 30px\">Remove-Item .SID.txt<\/p>\n<p style=\"padding-left: 30px\">}\nFollowing is the complete and working script. Although it will generate errors if there is an issue with the accounts being removed, it will function on all others. These errors are reported, and the script will continue to run to completion. I have used <strong>Bold<\/strong> formatting to highlight the sections that have been added. _____________________________________________________________<\/p>\n<p style=\"padding-left: 30px\"><strong>Set-Location $homedesktop<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>New-Item -path .over30dayprofiles.txt -itemtype File<\/strong><\/p>\n<p style=\"padding-left: 30px\">IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;) {<\/p>\n<p style=\"padding-left: 30px\">$over30dayprofiles = @(Get-ChildItem -force &#8220;C:Documents and Settings&#8221; | Where {<\/p>\n<p style=\"padding-left: 30px\">((Get-Date)-$_.lastwritetime).days -ge 30<\/p>\n<p style=\"padding-left: 30px\">} )<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;) {<\/p>\n<p style=\"padding-left: 30px\">$over30dayprofiles = @(Get-ChildItem -force &#8220;C:Users&#8221; | Where {<\/p>\n<p style=\"padding-left: 30px\">((Get-Date)-$_.lastwritetime).days -ge 30<\/p>\n<p style=\"padding-left: 30px\">} )<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Set-Content &#8220;.over30dayprofiles.txt&#8221; $over30dayprofiles<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$over30dayprofiles = @(Get-Content &#8220;.over30dayprofiles.txt&#8221; | Foreach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;$_ -replace &#8216;Administrator&#8217;, &#8221; `<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216;LocalService&#8217;, &#8221; `<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216;NetworkService&#8217;, &#8221;`<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216;desktop.ini&#8217;, &#8221;`<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216;temp&#8217;, &#8221;`<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216;All Users&#8217;, &#8221;`<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216;Default&#8217;, &#8221;`<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216;Default User&#8217;, &#8221;`<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216;Public&#8217;, &#8221;`<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -replace &#8216; User&#8217;, &#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">} | Select-String -Pattern &#8220;w&#8221; | ForEach-Object { $_.line })<\/p>\n<p style=\"padding-left: 30px\"><strong>If ($over30dayprofiles.count -eq 0){Write-Host &#8220;all profiles have been accessed in the last 30 days&#8221;}<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>If ($over30dayprofiles.count -gt 0){<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>$count = $over30dayprofiles.count<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;) {<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>get-WMIobject win32_userprofile | select SID, Localpath | Out-File .ProfileInfo.txt<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>$i = 0<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>New-Item -path .SID.txt -itemtype File<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>DO {<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>Get-Content .ProfileInfo.txt | Select-String -pattern $over30dayprofiles[$i] | ForEach-Object { <\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $_ -split &#8216;s+&#8217; | select -f 1<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } | Add-Content .SID.txt<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>$i++<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>}<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>&nbsp;<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>Until($i -ge $count)<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>$SID = @(Get-Content .SID.txt)}<\/strong><\/p>\n<p style=\"padding-left: 30px\">IF ((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;) {Set-Location &#8216;C:Documents and Settings&#8217;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">Do {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;5*&rdquo;){<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &nbsp;Remove-Item -force -recurse $over30dayprofiles[$i]<\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;IF((Get-WmiObject Win32_OperatingSystem).version -like &ldquo;6*&rdquo;) {<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &nbsp;<strong>$profile = Get-WmiObject win32_userprofile -filter &#8220;SID=&#8217;$($SID[$i])'&#8221;<\/strong><\/p>\n<p style=\"padding-left: 30px\">&nbsp; &nbsp;<strong>$profile.Delete()<\/strong><\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$i++<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">Until($i -ge $count)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><strong>Remove-Item .over30dayprofiles.txt<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>Remove-Item .ProfileInfo.txt<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>Remove-Item .SID.txt<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>}<\/strong>\nThe complete script is uploaded to the Script Center Repository: <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Over-30-day-removal-c27fbce9\" target=\"_blank\">Over 30 Day Removal<\/a>. Please download it from there, rather than attempting to copy from this web page. As with my previous posts, I welcome all critiques. It is my firm belief that anything can be improved by the collective<strong> <\/strong>knowledge of the community.\n~Bob\nThank you, Bob, for your time and efforts in writing this blog to share with our readers.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Guest blogger, Bob Stevens, revises his script for conditional user profile removal. Microsoft Scripting Guy, Ed Wilson, is here. I am happy to welcome back guest blogger, Bob Stevens. Take it away Bob&hellip; Previously I released a blog post entitled Weekend Scripter: Use PowerShell for Conditional User Profile Removal. If you have not read [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[424,16,340,56,3,45],"class_list":["post-2861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-bob-stevens","tag-desktop-management","tag-general-mgmt-tasks","tag-guest-blogger","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Guest blogger, Bob Stevens, revises his script for conditional user profile removal. Microsoft Scripting Guy, Ed Wilson, is here. I am happy to welcome back guest blogger, Bob Stevens. Take it away Bob&hellip; Previously I released a blog post entitled Weekend Scripter: Use PowerShell for Conditional User Profile Removal. If you have not read [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2861"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2861\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}