Windows Virtual Desktop (Windows 10 Multi-Session) PS Script

I am trying to clear out everyone’s download folder that logs into our WVD which we have set 15 users. I run this PS script but it only clears the download folder for who is logged in. Remove-Item -Path $env:UserProfile\Downloads*.* -Recurse -Force I am sure it just needs tweaking but I can’t seem to figure it out. Any help would be great thanks!


Also new to PS

how are you running your script ?

If users folder is in C:\Users, you can do this

$users = Get-ChildItem C:\Users -Depth 0

foreach ($user in $users) {
$user = $user.FullName
remove-item -path  "$user\downloads\*.*" -recurse -force

Note that you must run script as admin or system.

I thought about the same solution as you, but not having familiarity with profiles in a VDI MultiSession environment, this threw me off somewhat:

“I run this PS script but it only clears the download folder for who is logged in”

This implied to me that maybe the profiles are not similar, like they are virtual and generated only when a user is logged in??

This is probably not the case - most likely the profiles are standard Roaming User Profiles delivered from Microsoft’s servers. The problem is the path in edwardscott27’s command:

Remove-Item -Path $env:UserProfile\Downloads\*.* -Recurse -Force

which relies on $env:UserProfile. This is an environment variable that always points to the current user’s home directory, so this command will only ever apply to the current user as it is written.

If there is access to the user profile storage directory in this WVD environment, then sasaz12’s approach should work. The actual path to the directory could be verified like this:

$env:UserProfile | Split-Path -Parent

(in case the profiles aren’t stored in C:\Users)

However, I’m not familiar with the specifics of WVD. It may be that each WVD session has only the profile of the current user who initiated that session, in which case sasaz12’s script won’t do any good because there’s only one profile in C:\Users anyway. If this is the case, then kvprasoon’s question is very important:[quote quote=259237]how are you running your script ?[/quote]
To amplify, edwardscott27, when you run this command where are you running it from? Is it from an active WVD session? That session probably only has access to your profile. Do you have administrative access to the server that provides the WVD sessions? If you want to clear files for all users, you’ll need to do it from the server side where the user profiles are actually stored (or from a remote session with admin privileges on that server).

Alternatively, if you are able to set up logoff scripts you can implement the original command in such a script and have it execute whenever one of your users logs out of their WVD session, which should accomplish the goal.

It very much depends on what kind of administrative control you have over the WVD environment for your users.

Thanks for the info grokkit.

I wrote a script a while back to sync roaming profiles … I wonder if any of this would work/help?

$ProfilesOnSystem = Get-WmiObject -ClassName 'Win32_UserProfile'

ForEach($Profile In $ProfilesOnSystem) {
If($Profile.RoamingConfigured -eq $True) {
$localPath = $Profile.LocalPath
$remotePath = $Profile.RoamingPath
Remove-Item -Path $localPath\Downloads\*.* -Recurse -Force
Remove-Item -Path $remotePath\Downloads\*.* -Recurse -Force

Not sure how to get rid of the html chars, it should be ‘Win32_UserProfile’

Read the guide