Specific Folders Listing inside User Profiles

Hi PowerShell Gurus,

I have written down following PS Script to List all folder inside ‘C:\Users%UserProfile%\AppData\Local\Microsoft’. When I run it, it also searches ‘C:\Users%UserProfile%\AppData\Roaming\Microsoft’.
Here is code snippet…

$Profiles = Get-ChildItem -Path C:\Users\* -Force | Where-Object {($_.Name -notcontains 'Public') -and ($_.Name -notcontains 'trbac') -and ($_.Name -notcontains 'All Users') -and ($_.Name -notlike 'default') -and ($_.Name -notcontains 'Default User') -and ($_.Name -notcontains 'Desktop.ini')}
$Directory =  Join-Path -Path $Profiles.PSPath -ChildPath '\AppData\Local\Microsoft' 
if (Test-Path -Path $Directory -PathType Container) 
{ForEach-Object {Get-ChildItem -Path $Directory -Force -Exclude 'Application Data', 'History', 'Temporary Internet Files'}}

How do I exclude path ‘C:\Users%UserProfile%\AppData\Roaming\Microsoft’ given the fact that is mentioned in script block.

Can you add a line after #3 to output the contents of $Directory and confirm that it’s \AppData\Local?

The only other thing I can think of is that there’s a hardlink under \Local that points to Roaming, which basically includes the latter within the former. If thagt’s the case, you’d have to filter out anything containing \Roaming\ (e.g., “Where Path -notlike “*\Roaming*” or something).

Yep, what DonJ says.
This was kind of an odd ball thing, so I tried it on a couple of VM’s even modified it a bit as a sanity check, though the change should not have had any real bearing.

Meaning, I changed your -notcontains usage to…

($Profiles = Get-ChildItem -Path C:\Users\* -Force | 
Where-Object {$_.Name -notmatch'Public|trbac|All Users|default|Default User|Desktop.ini'}).PSPATH

($Directory =  Join-Path -Path $Profiles.PSPath -ChildPath '\AppData\Local\Microsoft')

The above of course just populates the vars and spits out the results to the screen as well.

Either way, no roaming UNC ever is shown. So, it’s environment on your end, heeding back to that potential hardlink deal already mentioned. Which would still seem a rather odd thing to do in a user profile tree.

Here is content once line # 1, 2 and 3 executes;


Interestingly, its behavior on production server is fine. It lists them all. it errors out on test server(due to hard links maybe)
Going forward, my intention is to do following if listing works fine (which seems to be working fine on Production server). I have changed path to ‘\AppData\Local\Microsoft\Terminal Server Client\Cache*’. Intent is to clean those files as they occupy bit of space. I will be setting it up inside Task Scheduler for automation.

$Profiles = Get-ChildItem -Path C:\Users\* -Force | Where-Object {($_.Name -notcontains 'Public') -and ($_.Name -notcontains 'trbac') -and ($_.Name -notcontains 'All Users') -and ($_.Name -notlike 'default') -and ($_.Name -notcontains 'Default User') -and ($_.Name -notcontains 'Desktop.ini')}
$Directory =  Join-Path -Path $Profiles.PSPath -ChildPath '\AppData\Local\Microsoft\Terminal Server Client\Cache\*'
if (Test-Path -Path $Directory -PathType Container) {ForEach-Object {Remove-Item -Path $Directory -Force -Exclude 'Application Data', 'History', 'Temporary Internet Files' -Confirm:$False}}

I ran it even by forcing an error like changing folder ‘Cache’ folder something else so that it does not stop processing if it does not find ‘Cache’ folder. It breezed and cleaned files inside ‘Cache’ folder available in other %userprofile%.

Thanks for your(both) support

That if statement will always return true if there’s more than one $Directory, since an array is always true. By the way, you can use wmi to get a list of the profiles:

Get-WmiObject win32_userprofile | where { -not $_.special } | select -expand localpath  | 
  foreach { "$_\AppData\Local\Microsoft" }


It doesn’t list Roaming on my computer.

I came across a weird issue when setting up task schedule of the same script. It runs fine directly from PowerShell ISE .However, when setup to run from Task Schedule , it does not delete contents from

‘\AppData\Local\Microsoft\Terminal Server Client\Cache*’
while it deletes from

is it because of the fact that ‘\AppData\Local\Microsoft’ is system protected folder or some sort of prompt is appearing during script execution;

$Profiles = Get-ChildItem -Path C:\Users\* -Force | Where-Object {($_.Name -notcontains 'Public') -and ($_.Name -notcontains 'trbac') -and ($_.Name -notcontains 'All Users') `
-and ($_.Name -notlike 'default') -and ($_.Name -notcontains 'Default User') -and ($_.Name -notcontains 'Desktop.ini')}
$Directory =  Join-Path -Path $Profiles.PSPath -ChildPath '\AppData\Local\Microsoft\Terminal Server Client\Cache\*' 
$Directory2 = Join-Path -Path $Profiles.PSPath -ChildPath '\AppData\Local\VMware\vpx\*'
if (Test-Path -Path $Directory1 -PathType Container) {ForEach-Object {Remove-Item -Path $Directory1 -Force  -Confirm:$False}}
if (Test-Path -Path $Directory2 -PathType Container) {ForEach-Object {Remove-Item -Path $Directory2 -Include *.log -Force -recurse -Confirm:$False}}

any idea?