A little PowerShell script to delete content

Nothing fancy but something I thought I need to share. A customer asked me to create a solution for left-over-files on laptops. Their laptops should not have local files at all but in some circumstances someone might need to create some. They operate in areas with limited to no Internet connection. Access to the virtual desktop is not possible from those locations and so they asked for a solution.


The laptops are managed with VMware AirWatch and deployed with RES ONE Automation. Both great tools but not when you want to remove files and have additional requirements. The laptops are not equipped with a TPM chip so the files are not encrypted when on the laptop, of course they can but a password is not an option here.. End users are encouraged to not work locally but use the virtual environment. The laptop only has Microsoft Word, Excel and PowerPoint running.


  • End users should not be able to save files anywhere then in Documents or on the desktop.
  • Files should be removed every day at first logon.

The first requirement is still being worked on, several options are available. We are looking for the best manageable solution. The second part I worked on and tested today, I wrote a little PowerShell script, nothing fancy, that does the job. I thought I’d share it with you perhaps it is of use. I’ll add some text between the lines to explain what I did and why, of course if you ever made a script yourself you understand at a glance.

The Script + Explanation

$FileName= (Get-Date -Format ddMMyyyy) + “.txt”

>> Files had to be deleted every day, first logon, to make sure I can check if that first logon has passed I decided to create a file with the date of the day <<

$Yesterday = (Get-Date).AddDays(-1).ToString(‘ddMMyyyy’)

>> Of course some house cleaning is nice as well, so I want to know yesterdays date as well <<

$Location = “C:\temp\”

>> The file to check for the first boot is stored in the temp folder <<

If (Test-Path ($Location + $FileName)){

  # // File exists, first boot has passed

>> I check if the file is there, in the Temp folder and if it is first boot/logon has passed and the script closes. <<


  # // File does not exist, first boot

>> If it is the first boot/logon we need to set a new file <<

  New-Item -type file -Name $FileName -force -Value “1st boot” -Path $Location

>> Creating a new file with the name that corresponds with the date of today  in the Temp folder <<

  Remove-Item -Path $env:USERPROFILE\Documents\*.*

>> Remove files in MyDocument and all subfolder <<

>> Did not use Get-Childitem here but closed the creation of folder/files in the root as they have Secure Content Locker Sync running <<

  Get-ChildItem -Path $env:USERPROFILE\Desktop\*.* -Exclude *.lnk -Recurse | Remove-Item

>> Changed this line as -recursive wasn’t working trying to delete files on the desktop as well as in all folder on the desktop <<

>> I noticed that while delete-item deleted everything it did not delete files in folders and I think users will also figure that out soon enough. <<

Remove-Item ($Location + $Yesterday + “.txt”)

>> did some cleanup and remove the file from yesterday <<


And that’s it, add Set-executionpolicy in front of it, placed a cmd file to start it all in the startup folder and it works flawlessly. Nothing fancy but it does the job. I looked at different options to make sure the script would always start but a lot of options depend on the laptop being connected to the network. This seemed to be the best or most effective way.

One thought on “A little PowerShell script to delete content”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.