Created
February 24, 2024 11:43
-
-
Save davidjenner/fe047f55fcad86e8f81221a5f711a060 to your computer and use it in GitHub Desktop.
AutomatePCrefresh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ideas on automating a PC Refresh | |
Write a powershell script to automate at job on the network every monday at midnight | |
# Define the action to be performed (replace with your own command) | |
$scriptPath = "C:\Path\To\Your\Script.ps1" | |
# Define the task name | |
$taskName = "MyScheduledTask" | |
# Create a new scheduled task action | |
$action = New-ScheduledTaskAction -Execute "PowerShell" -Argument "-NoProfile -ExecutionPolicy Bypass -File $scriptPath" | |
# Create a daily trigger that repeats every 7 days (for Mondays) | |
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 12:00AM | |
# Register the scheduled task | |
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -User "DOMAIN\Username" -Password "YourPassword" | |
Write-Host "Scheduled task '$taskName' created successfully." | |
============================================= | |
Write a powershell script to clear all the user profiles off the 10 most full PCs on a computer network | |
# Define the path to the file containing the list of computer names | |
$computerListPath = 'C:\Path\To\Your\ComputerList.txt' | |
# Read the computer names from the file | |
$computerNames = Get-Content $computerListPath | |
# Sort the computers by available disk space (descending order) | |
$sortedComputers = $computerNames | ForEach-Object { | |
$computer = $_ | |
$diskSpace = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computer | | |
Where-Object { $_.DriveType -eq 3 } | | |
Sort-Object -Property FreeSpace -Descending | | |
Select-Object -First 1 | |
[PSCustomObject]@{ | |
ComputerName = $computer | |
FreeSpaceGB = [math]::Round($diskSpace.FreeSpace / 1GB, 2) | |
} | |
} | Sort-Object -Property FreeSpaceGB -Descending | |
# Take the top 10 computers with the least free space | |
$top10Computers = $sortedComputers | Select-Object -First 10 | |
# Loop through the top 10 computers and remove user profiles | |
foreach ($pc in $top10Computers) { | |
Write-Host "Clearing profiles on $($pc.ComputerName)..." | |
Get-CimInstance -ComputerName $pc.ComputerName -Class Win32_UserProfile | | |
Where-Object { !$_.Special -and $_.Loaded -eq $false } | | |
Remove-CimInstance -WhatIf | |
} | |
Write-Host "User profiles cleared on the top 10 most full PCs." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment