*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***
Product: PowerShell Studio 2020 (64 Bit)
Build: v5.7.172
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17763.0
*** Please add details and screenshots as needed below. ***
I have a script that upon completion the users want the option to start over from the top.
I am working on the procedure RestartOrQuit-ComputerClean at the last line of code.
From ordinary ISE, I can just call the file name to start over...
What is the code to get the script to start over if a user selects run again?
- <#
- This script is a comprehensive cleaning tool that can clean one or many machines.
- If you enter in a single machine name the machine will be cleaned
- #InputOne - either the machine name or HP-MXL4* If the * is used, the script will loop through all machines that match a like value. Note: the original script broke the
- inputs into multple inputs. This has been removed in favor of a single input using the * at strategic points.
- $BaseDrive - hard coded to C drive
- $ComputerName - the name of the computer being cleaned.
- $EmployeeID the primary user of the Computer
- 29-JAN-2020
- Work on Provision to Restart the Job or Quit
- 16-JAN-2020
- Add Clean SWSetup Folder - Complete
- Add a Provision to Restart the Job on Another Computer or Quit
- Create Individual Log Files for Each Job
- Launch Windows Explorer <MachineName>\c$\users - for users to delete any user profiles
- 13-NOV-2019
- Use code from the Comprehensive Terminal Cleaner to clean
- TCA, MyUser, ThinPC
- Added a note that if a thin client device add svcthinpc as the primary user.
- 09-SEP-2019
- Put an notice the app has completed. Scroll Up to Review Log. Click File Exit when done.
- Compiled for Release to Service Desk
- Added DelProf to clean up any unused profiles
- Added Get Free Disk Space to provide available disk space after cleaning.
- 17-SEP-2019
- Begin work to integrate the terminal cleaning tool into the overall computer clean tool.
- 06-SEP-2019
- Adding Delpfrof to clean up rogue profiles
- 05-SEP-2019
- Saved V9 as V10
- Put a forEach Loop in solved the problem
- $deleteFiles = Get-Childitem $fullTargetPath -Recurse |
- Where {$_.LastWriteTime -lt (Get-Date).AddDays(-10)} |
- Foreach { Remove-Item $_.FullName -Force}
- https://stackoverflow.com/questions/20640747/powershell-remove-item-does-not-delete-all-items
- 22-Aug-2019
- Cleaned up v6 saved as v8
- Renaming some procedures to make them more clear.
- Importing the prodecures from v7 which validate the user has a folder on c:\users\<username>
- 28-JUN-2019
- Setup Clean-Junk function to accept parameters.- need to decide if hard code paths to check.. or use a txt file to read and loop through.
- Pullin gout PSSession stuff...
- 21-JUN-2019
- Create and Work on Function PSSession and Enter PSSEssion - This will speed up performance for the deletes.
- Modified the clean procedure name to Clean-Junk
- Modified the procedure to be able to accept inputs. I have some other code that does this - Need to see if I can get it to work here.
- 11-Jun-2019
- Got Rid of the Input Function - broke each one of the three inputs down into it's own function.
- Makes things much more modular.
- 07-Jun-2019
- The Function Seems to Work Correctly - however - not from within a function..
- Need to Validate CleanDays Variable is a numberic value
- #>
- <#
- Define Variables Here
- $script:ComputerName - The computer name we are checking
- $script:EmployeeId - The Employee ID That we are checking against the computer
- $script:ProfileLocation - \\Computername\c$\users\EmployeeID
- $Script:PreCleanSize - Use This to Calculate the size of the area to be cleaned -
- $Script:PostCleanSize - Use This To Calculate the size after cleaning -
- $script:CleanAgeDays - pass this along to make sure we don't clean files less than a certain age.
- $Script:session - Assigns a variable to the Remote Session so the Remote Session is easier to work with.
- $script:delprofloc - adding delprof to clean up inactive windows profiles
- $script:FreeDiskpsace - Get the availble Diskspace After Clean
- #>
- # Location Variables
- $script:delprofloc = "\\dc1-p-a-scsmss1\portal-tools$\Citrix Utilities\WindowsCleanUpTool\delprof\"
- Function PreCleanSize { $Script:PreCleanSize = "{0:N2}" -f ((Get-ChildItem -Path $Script:FolderToClean -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB) }
- Function PostCleanSize { $Script:PostCleanSize = "{0:N2}" -f ((Get-ChildItem -Path $Script:FolderToClean -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB) }
- Function Get-ComputerName
- {
- $script:ComputerName = Read-Host -Prompt "Input The Machine Name That Needs Cleaning"
- Write-Host "Checking Computer Name Against Active Directory.. Hang ..."
- TRY
- {
- (Get-ADComputer -Identity $script:ComputerName) |
- clear-host
- Write-Host "Congratulations Computer $script:ComputerName Successfully Found In Active Directory - Moving On"
- clear-host
- Confirm-ComputerOnline
- }
- Catch
- {
- Clear-Host
- Write-Host "Computer $script:ComputerName Not Found In Active Directory Try Again."
- Get-ComputerName
- }
- }
- Function Get-EmployeeID
- {
- $script:EmployeeId = Read-Host -Prompt "Input The EmployeeID of Primary User Of $script:ComputerName Computer. Note: If a thin client device enter SVCTHINPC. If unsure of the primary user enter your E#."
- Write-Host "Checking EmployeeID Against Active Directory.. Hang ..."
- TRY
- {
- (Get-ADuser -Identity $script:EmployeeId)
- clear
- Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
- Write-Host "Computer $script:ComputerName was online at last test."
- Write-Host "Congratulations Employee $script:EmployeeId Successfully Found In Active Directory - Moving On"
- }
- Catch
- {
- Clear-Host
- Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
- Write-Host "Employee $script:EmployeeId Not Found In Active Directory Try Again."
- If ($script:EmployeeId -eq "tca") { break
- Match-EmployeeWithComputer }
- Get-EmployeeID
- }
- }
- function Get-CleanDays
- {
- $script:CleanAgeDays = Read-Host -Prompt "Clean Files Older than (Recomended Value Between 60 and 90)"
- if ($script:CleanAgeDays -match '^\d+$') { write-host "Congratulations You Entered A Numeric Value" }
- Else
- {
- Clear-Host
- $script:CleanAgeDays = Read-Host -Prompt "Clean Files Older than (Recomended Value Between 60 and 90)"
- CleanAge-Validation
- }
- }
- Function Perform-Validation
- {
- Clear-Host
- Write-Host "You want to clean machine $script:ComputerName and the primary user is $script:EmployeeID and you are cleaning files older than $script:CleanAgeDays Days."
- $YesNo = Read-Host -Prompt "Please confirm you want to proceed. Enter Y to continue or N to start over"
- switch ($YesNo)
- {
- "Y" {
- Write-Host "Confirming $script:EmployeeId has a user profile directory on $script:ComputerName.";
- Match-EmployeeWithComputer
- }
- "N" {
- echo "You Need To Start Over";
- Clear-Host
- Get-ComputerName
- Get-EmployeeID
- Get-CleanDays
- }
- default { echo "You Need to Enter Y or N"; Perform-Validation }
- }
- }
- Function Confirm-ComputerOnline
- {
- clear-host
- Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
- Write-Host "Checking to see if $script:ComputerName is online Testing, testing, 1...2...3...."
- TRY
- {
- (test-connection -computername $script:ComputerName -quiet)
- Clear-Host
- Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
- Write-Host "Computer $script:ComputerName is online. Moving On To The Next Step."
- }
- Catch
- {
- Write-Host "This Computer is Offline. Abort Script"
- }
- }
- function Match-EmployeeWithComputer
- {
- $script:ProfileLocation = "\\" + $script:Computername + "\c$\users\" + $script:EmployeeId
- If (test-path -path $script:ProfileLocation)
- {
- Clear-Host
- Write-Host "Employee ID $script:EmployeeId has a user profile on $script:ComputerName."
- Write-Host "Time to Start Cleaning $script:Computername."
- }
- Else
- {
- clear-host
- Write-Host "Employee ID $script:EmployeeId does not Exist on the C Drive."
- Write-Host "We will not be able to clean this area as it does not exist."
- Write-Host "Please check the c:\users folder on " $script:Computername ". We will clean other areas of the computer"
- }
- }
- function Clean-Temp
- {
- $Script:FolderToClean = "\\$Script:ComputerName\c$\temp\"
- Write-Host "Testing for a temp folder of the root of c: drive"
- if ($(Try { Test-Path $Script:FolderToClean.trim() }
- Catch { $false }))
- {
- # Clear-Host
- Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
- PreCleanSize
- Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
- Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
- #Updated Code to V10
- cd $Script:FolderToClean
- Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
- Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
- PostCleanSize
- Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
- $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
- If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
- Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
- }
- Else
- {
- write-host "$Script:FolderToClean Path not found. No worries! Moving On."
- }
- }
- function Clean-WindowsTemp
- {
- $Script:FolderToClean = "\\$Script:ComputerName\c$\Windows\temp\"
- Write-Host "Testing for a c:\windows\temp folder of the root of c: drive"
- if ($(Try { Test-Path $Script:FolderToClean.trim() }
- Catch { $false }))
- {
- # Clear-Host
- Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
- CD $Script:FolderToClean
- PreCleanSize
- Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
- Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
- #Updated Code to V10
- Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -recurse -ErrorAction SilentlyContinue }
- Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
- PostCleanSize
- Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
- $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
- If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
- Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
- }
- Else
- {
- write-host "$Script:FolderToClean Path not found. No worries! Moving On."
- }
- }
- function Clean-ccmcache
- {
- # v10 updated with the foreach update
- $Script:FolderToClean = "\\$Script:ComputerName\c$\Windows\ccmcache\"
- Write-Host "Testing for a c:\windows\ccmcache folder of the root of c: drive"
- if ($(Try { Test-Path $Script:FolderToClean.trim() }
- Catch { $false }))
- {
- # Clear-Host
- Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
- CD $Script:FolderToClean
- PreCleanSize
- Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
- Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
- #Updated to V10 --- Note: Delete ALL files from this directory - so the date parameter is removed.
- Get-Childitem $Script:FolderToClean -Recurse | Foreach { Remove-Item $_.FullName -Force -Recurse }
- Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
- PostCleanSize
- Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
- $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
- If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
- Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
- }
- Else
- {
- write-host "$Script:FolderToClean Path not found. No worries! Moving On."
- }
- }
- function Clean-SWSetup
- {
- # v10 updated with the foreach update
- $Script:FolderToClean = "\\$Script:ComputerName\c$\SWSetup\"
- Write-Host "Testing for a c:\SWSetup folder of the root of c: drive"
- if ($(Try { Test-Path $Script:FolderToClean.trim() }
- Catch { $false }))
- {
- Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
- CD $Script:FolderToClean
- PreCleanSize
- Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
- Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
- #Updated to V10 --- Note: Delete ALL files from this directory - so the date parameter is removed.
- Get-Childitem $Script:FolderToClean -Recurse | Foreach { Remove-Item $_.FullName -Force -Recurse }
- Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
- PostCleanSize
- Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
- $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
- If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
- Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
- }
- Else
- {
- write-host "$Script:FolderToClean Path not found. No worries! Moving On."
- }
- }
- function Clean-usertemp
- {
- $Script:FolderToClean = "\\$Script:ComputerName\c$\users\" + $script:EmployeeId + "\AppData\Local\Temp"
- Write-Host "Testing for a user's temp folder of the root of c: drive"
- if ($(Try { Test-Path $Script:FolderToClean.trim() }
- Catch { $false }))
- {
- # Clear-Host
- Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
- CD $Script:FolderToClean
- PreCleanSize
- Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
- Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
- #Updated to V10
- Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
- Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
- PostCleanSize
- Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
- $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
- If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
- Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
- }
- Else
- {
- write-host "$Script:FolderToClean Path not found. No worries! Moving On."
- }
- }
- function Clean-TCAusertemp
- {
- $Script:FolderToClean = "\\$Script:ComputerName\c$\users\tca" + "\AppData\Local\Temp"
- Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
- if ($(Try { Test-Path $Script:FolderToClean.trim() }
- Catch { $false }))
- {
- # Clear-Host
- Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
- CD $Script:FolderToClean
- PreCleanSize
- # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
- Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
- #Updated to V10
- Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
- Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
- PostCleanSize
- # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
- $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
- If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
- Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
- }
- Else
- {
- write-host "$Script:FolderToClean Path not found. No worries! Moving On."
- }
- }
- function Clean-SVCThinpcUserTemp
- {
- $Script:FolderToClean = "\\$Script:ComputerName\c$\users\svcthinpc" + "\AppData\Local\Temp"
- Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
- if ($(Try { Test-Path $Script:FolderToClean.trim() }
- Catch { $false }))
- {
- # Clear-Host
- Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
- CD $Script:FolderToClean
- PreCleanSize
- # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
- Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
- #Updated to V10
- Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
- Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
- PostCleanSize
- # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
- $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
- If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
- Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
- }
- Else
- {
- write-host "$Script:FolderToClean Path not found. No worries! Moving On."
- }
- }
- function Clean-MyUsertemp
- {
- $Script:FolderToClean = "\\$Script:ComputerName\c$\users\MyUser" + "\AppData\Local\Temp"
- Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
- if ($(Try { Test-Path $Script:FolderToClean.trim() }
- Catch { $false }))
- {
- # Clear-Host
- Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
- CD $Script:FolderToClean
- PreCleanSize
- # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
- Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
- #Updated to V10
- Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
- Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
- PostCleanSize
- # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
- $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
- If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
- Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
- }
- Else
- {
- write-host "$Script:FolderToClean Path not found. No worries! Moving On."
- }
- }
- function Run-DelProf2
- {
- #http://www.edugeek.net/forums/scripts/170719-quick-dirty-powershell-script-delprof2.html
- Write-Host "Removing any inactive Windows profiles from $script:ComputerName"
- & '\\dc1-p-a-scsmss1\portal-tools$\Citrix Utilities\WindowsCleanUpTool\delprof\DELPROF2.EXE' /u /ed:$script:EmployeeId /q /C:\\$script:ComputerName
- Write-Host "Inactive Profiles Removed from $script:ComputerName"
- }
- Function Get-FreeDiskSpace
- {
- Write-Host "Getting available disk space on $script:ComputerName"
- $FreediskSpace = Get-WmiObject Win32_LogicalDisk -ComputerName $script:ComputerName -Filter "DeviceID='C:'" |
- Foreach-Object { [Math]::Round($_.FreeSpace /1GB) }
- Write-Host "$script:ComputerName has $FreediskSpace gigabytes available after cleaning."
- }
- Function Display-FinalInstructions {
- Write-Host "Contratulations! You have successfully cleaned $script:ComputerName. Scroll up to review the Log. "
- Write-Host "Click File | Exit to close the application if you are satisfied with the cleanup results."
- }
- Function RestartOrQuit-ComputerClean {
- Write-Host "To clean another computer without exiting the application type Y at the prompt."
- Write-Host "To close the application and do something else type N at the prompt."
- $yesno = Read-Host -Prompt "Do you want to clean another computer or thin client device or Quit the Application?"
- $yesno = switch ($quitornot) {
- "Y" {"Restarting to clean another computer."; break}
- "N" {"We are quitting the application."; break}
- default {Write-Host "To clean another computer without exiting the application type Y and the Prompt"
- Write-Host "To close the application and do something else typy N" ; break}
- }
- $quitornot
- }
- Import-Module ActiveDirectory
- Clear-Host
- Get-ComputerName
- Get-EmployeeID
- Match-EmployeeWithComputer
- Get-CleanDays
- Perform-Validation
- Clean-Temp
- Clean-WindowsTemp
- Clean-ccmcache
- Clean-SWSetup
- Clean-usertemp
- Clean-TCAusertemp
- Clean-SVCThinpcUserTemp
- Clean-MyUsertemp
- Run-DelProf2
- Get-FreeDiskSpace
- # Display-FinalInstructions
- RestartOrQuit-ComputerClean
DO NOT POST LICENSES, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM