Skip to content

Instantly share code, notes, and snippets.

@jmconway
jmconway / Remove-LocalUserProfile.ps1
Created May 10, 2021 15:27
Old PowerShell script used for removing cached local user profiles from a machine. Takes a parameter "Threshold" as integer such that profiles older than $Threshold days will be deleted. Uses WMI which has fallen out of fashion for CIM, but should still work. The delete method properly clears the registry and C:\Users as if done from the System …
# Clean User Profiles
# Makes WMI call to list profiles, excluding system accounts
# Deletes profiles whose LastUseTime is older than a set number of days
Param(
# Where-Object $Threshold is number of days as an integer; Profiles older than $Threshold days will be deleted
[Parameter(Position=0,Mandatory=$true)]
[int]$Threshold
)
@jmconway
jmconway / Clear-WSUSConfig.ps1
Created May 10, 2021 15:31
Quick PowerShell function that I use in some scripts to overwrite tattooed registry settings from a prior WSUS implementation.
function Clear-WSUSConfig {
Stop-Service wuauserv -force
Set-ItemProperty -Path HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name UseWUServer -Value 0
Start-Service wuauserv
}
@jmconway
jmconway / Enable-RSAT.ps1
Created May 10, 2021 15:37
A PowerShell script I used to enable Remote Server Administration Tools on secure management workstations. First there's a function to clear our prior WSUS implementation, so one may want to cut that part out if it's not applicable to their environment. Next there's a function to allow getting components/optional features from Windows Update. Fi…
# Now that we don't run our own WSUS, clear WSUS config from registry if causing conflicts.
function Clear-WSUSConfig {
Stop-Service wuauserv -force
Set-ItemProperty -Path HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name UseWUServer -Value 0
Start-Service wuauserv
}
# Any errors encountered even when running as admin usually require manually changing Windows Update/Component servicing settings
@jmconway
jmconway / Clear-CliHistory.ps1
Created May 10, 2021 15:40
Clear the Microsoft PowerShell console history in your terminal.
[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()
@jmconway
jmconway / Show-UserWarning.ps1
Last active May 11, 2021 18:10
Basic user logon script to show a warning window using a bit of .NET. My use case initially was just to get users to acknowledge the prompt, so my "exit" command can be replaced with commands you want run depending on what the user selects. See Microsoft Docs article in the comments for more on prompt options with .NET.
#-----------------------------------------------
# Warning Prompt - Windows User Logon Script
#-----------------------------------------------
# Set some variables that we'll use to craft the prompt in the next section
$message = ""
$title = ""
# We'll use a bit of .NET framework here...
# First, we have to add the assembly as it does not exist by default in PowerShell
@jmconway
jmconway / Show-TwoStepUserWarning.ps1
Created May 11, 2021 18:14
Basic user logon script to show a warning window using a bit of .NET. Like my previous Show-UserWarning.ps1 gist, but in this case the goal was to link users to 2-Step enrollment self-serice before a specified deadline based on their selection on the prompt.
#-----------------------------------------------
# Two-Step Warning - Windows User Logon Script
#-----------------------------------------------
# Set deadline as a DateTime object - this is important as there are multiple ways Windows formats time
[datetime]$deadline = "05/01/2021 00:00"
# Set countdown as deadline minus the current date at time of logon
$countdown = $deadline - (Get-Date)
@jmconway
jmconway / Invoke-WindowsActivation.ps1
Created May 11, 2021 18:15
Quick WMI calls to set an MAK in Windows and activate at the end of WDS/MDT deployments.
$key = ''
$MAK = Get-WmiObject -Query "Select * from SoftwareLicensingService"
$MAK.InstallProductKey($key)
$MAK.RefreshLicenseStatus()
@jmconway
jmconway / Remove-AppxPackages.ps1
Created May 11, 2021 18:19
A basic script for removing Windows 10 AppX Packages and Provisioned Packages run during my WDS/MDT deployment. While I don't recommend sysprep anymore, note that in order to avoid issues with it or other operations, both related AppX packages and AppX provisioned packages need to be removed.
$provisioned = "Microsoft.BingWeather","Microsoft.GetHelp","Microsoft.Getstarted","Microsoft.Messaging","Microsoft.MicrosoftOfficeHub","Microsoft.MicrosoftSolitaireCollection","Microsoft.Office.OneNote","Microsoft.OneConnect","Microsoft.People","Microsoft.People","Microsoft.Wallet","microsoft.windowscommunicationsapp","Microsoft.WindowsFeedbackHub","Microsoft.WindowsMaps","Microsoft.YourPhone","Microsoft.ZuneMusic","Microsoft.ZuneVideo"
$appx = "Microsoft.Windows.PeopleExperienceHost","Microsoft.BingWeather","Microsoft.GetHelp","Microsoft.Getstarted","Microsoft.Messaging","Microsoft.MicrosoftOfficeHub","Microsoft.MicrosoftSolitaireCollection","Microsoft.Office.OneNote","Microsoft.OneConnect","Microsoft.People","Microsoft.SkypeApp","Microsoft.Wallet","microsoft.windowscommunicationsapps","Microsoft.WindowsFeedbackHub","Microsoft.WindowsMaps","Microsoft.YourPhone","Microsoft.ZuneMusic","Microsoft.ZuneVideo"
ForEach ($app in $provisioned) {
Get-AppxProvisionedPackage -Online | Where-Object {$_.PackageName
@jmconway
jmconway / Set-OptionalFeatures.ps1
Last active May 11, 2021 18:28
Windows PowerShell script to set Optional Features on the image deployed with WDS/MDT. Has a few parameters specific to my use cases, but otherwise the $toDisable and $toEnable variables include the FeatureName's of optional features I can universally disable in my deployments/environment.
param(
# Specify "-Legacy" if Internet Explorer is still needed for compatibility with apps
[Parameter(Mandatory=$false)]
[Switch]
$Legacy,
# Specify "-IncludeWSL" to enable Windows Subsystem for Linux for IT Staff or approved faculty
[Parameter(Mandatory=$false)]
[Switch]
$IncludeWSL,
@jmconway
jmconway / Disable-ConsumerExperience.ps1
Created May 12, 2021 17:00
Basic script used in WDS/MDT deployments to disable "Consumer Experiences" via the Registry on Windows 10 Enterprise editions.
# Disable Windows Consumer Experience via the Registry
$regPath = "HKLM:\\SOFTWARE\Policies\Microsoft\Windows\CloudContent"
$regName = "DisableWindowsConsumerFeatures"
$regValue = "1"
if (!(Test-Path -Path $regPath)) {
New-Item -Path $regPath
Try {
New-ItemProperty -Path $regPath -Name $regName -Value $regValue -PropertyType DWORD