Creating a Logon Script To Cleanup User Directories

In this post, I’ll discuss how I created a PowerShell script that runs when a user logs out of a terminal server and cleans up a directory in their home folder that was filling up with space due to application crashes.

This is the script I created:

$username = $ENV:USERPROFILENAME

$testpath = Test-Path -Path "$username\AppData\Local\Microsoft\Windows\ApplicationFolder"

if ($testpath -eq $true){

 $items = Get-ChildItem - Path "$username\AppData\Local\Microsoft\Windows\ApplicationFolder"

 foreach($i in $items){

  Remove-Item -Path "$username\AppData\Local\Microsoft\Windows\ApplicationFolder\$i" -Recurse -Confirm:$FALSE

 }

}else{}

This code will get the users profile root path and then check if the application folder exists, if it doesn’t then the script ends. If it does exist, the script will cycle through each entry and remove it.

The -Confirm:$FALSE parameter was added because the script kept asking for confirmation when deleting each item. This stops this behaviors and deletes each item without a confirmation prompt.

Now that I have the script and it is working as expected, I create a local group policy that will use:

Name – “powershell.exe”

Parameters – “-F “C:\path\to\file.ps1”

You can see this in the screenshot below:

Logoff script group policy

 

This group policy was added under:

User Configuration – Windows Settings – Scripts (Logon/Logoff) – Logoff

Hopefully you can replicate what I have done and don’t experience any issue. Note that you might need to change the script execution policy on the machine before this works properly. Just something to keep in mind if the group policy isn’t working. Enjoy!

SharePoint URL Changing

There are two main ways that I change the URL of a list or library on a SharePoint site. First off I’d like to explain why I often have to do these tricks. Whenever you create a new SharePoint site from a template, even after you change the names of all the lists and libraries in a web page, the URL will still reference what ever the template lists were called.

Option 1 – SharePoint Designer

To change the URL in SharePoint designer, open your site using the URL “http://HOSTNAME/sites/SITENAME” and this should give you list of items in the left hand column resembling the screenshot below:

SharePoint Designer Column

You should see that the bottom option says “All Files“, double click this and go into “Lists“. This will show all of the lists on your site and you should see that they are all using the old name which is the cause of the incorrect URLs. Simply rename these lists to what ever you need and the URL will change.

 

Option 2 – File Explorer

If you don’t see the “All Files” section in SharePoint designer then this method can be used instead. Open a file explorer and navigate to your site. You can see how I have done this below:

File Explorer

Now go into “Lists” and you should be able to see the incorrectly named lists. Simply rename these lists and the URL will change.

 

I’m not sure why the “All Files” section in SharePoint designer doesn’t show so If anyone can shed some light on that I would be appreciated. Enjoy!