Skip to content

Instantly share code, notes, and snippets.

View iyre's full-sized avatar

Miles Miller iyre

View GitHub Profile
@iyre
iyre / CleanupSharedPrinters.ps1
Last active February 20, 2020 22:25
Consolidate shared printers from various servers
$NewPrintServer = 'PRINT'
$ComObject = New-Object -ComObject WScript.Network
$Map = @{
"4071" = "4071"
"4111" = "4111N"
"5001" = "4111N"
"2640" = "2640N"
}
@iyre
iyre / Set-NewUpn.ps1
Created February 20, 2020 15:20
Batch replace domain name portion of UPN in active directory
# batch replace domain name portion of UPN in active directory
$SearchBase = "OU=Domain Users, DC=contoso, DC=com"
$OldDN = "contoso.com.local"
$NewDN = "contoso.com"
Import-Module ActiveDirectory
Get-ADUser -Filter { UserPrincipalName -like "*$OldDN" } -SearchBase $SearchBase |
ForEach-Object {
Write-Host $_.Name
$Upn = $_.UserPrincipalName.Replace( $OldDN, $NewDN )
@iyre
iyre / KillProcessActiveComputers.ps1
Last active July 31, 2020 20:08
Invoke commands on all active computers in a domain
$ProcessName = "outlook"
$DateOffset = 30
$StartDate = (Get-Date).AddDays(-($DateOffset))
$SearchBase = "OU=Domain Computers, DC=contoso, DC=com"
Get-ADComputer -Filter {
( LastLogonDate -gt $StartDate ) -and
( Name -ne "$env:COMPUTERNAME" )
} -SearchBase $SearchBase |
Foreach-Object {
@iyre
iyre / SutdownActiveComputers.ps1
Created February 18, 2020 23:41
Remotely shut down all active computers on a domain
# Shuts down computers without notice
$DateOffset = 30
$StartDate = (Get-Date).AddDays(-($DateOffset))
$SearchBase = "OU=Domain Computers, DC=contoso, DC=com"
Get-ADComputer -Filter {
( LastLogonDate -gt $StartDate ) -and
( Name -ne "$env:COMPUTERNAME" )
} -SearchBase $SearchBase -Properties LastLogonDate |
@iyre
iyre / ListActiveComputers.ps1
Created February 18, 2020 23:31
Get a list of active domain computers
$DateOffset = 30
$StartDate = (Get-Date).adddays(-($DateOffset))
$SearchBase = "OU=Domain Computers, DC=contoso, DC=com"
Get-ADComputer -Filter {
( LastLogonDate -gt $StartDate ) -and
( Name -ne "$env:COMPUTERNAME" )
} -SearchBase $SearchBase -Properties LastLogonDate |
Sort-Object -Property LastLogonDate |
Format-Table -Property Name, LastLogonDate
@iyre
iyre / ClearStuckPrintQueue.ps1
Last active February 20, 2020 16:37
Remotely clear print queue and restart spooler
$ComputerName = Read-Host -Prompt 'Target'
$Cred = Get-Credential -Credential $null
Write-Host "Opening session with remote host"
$Session = New-PSSession -ComputerName $ComputerName -Credential $Cred -ErrorAction Stop
$Confirm = Read-Host "Are you sure that you want to delete all print jobs on $ComputerName (Y/N)"
if ( $Confirm -eq 'Y' ) {
Write-Host "1. Stopping spooler" -ForegroundColor Green
@iyre
iyre / FindSharedPrinters.ps1
Created February 18, 2020 22:59
Check the registry for shared printers
$ComputerName = Read-Host -Prompt 'Host'
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
Get-ChildItem Registry::\HKEY_Users |
Where-Object { $_.PSChildName -notmatch ".DEFAULT|S-1-5-18|S-1-5-19|S-1-5-20|_Classes" } |
Select-Object -ExpandProperty PSChildName |
ForEach-Object { Get-ChildItem Registry::\HKEY_Users\$_\Printers\Connections -Recurse | Select-Object }
}
@iyre
iyre / FindExpiringPasswords.ps1
Created February 18, 2020 22:39
Query AD for expiring passwords
$ExcludeDays = 90 # Exclude if password changed in the last $ days
$CutoffDate = (Get-Date).adddays(-($ExcludeDays))
$Users = Get-ADUser -filter {
Enabled -eq $True -and
PasswordNeverExpires -eq $False -and
PasswordLastSet -lt $CutoffDate
} -Properties msDS-UserPasswordExpiryTimeComputed, PasswordLastSet, CannotChangePassword
$Users | Sort-Object -Property msDS-UserPasswordExpiryTimeComputed | Select-Object Name, PasswordLastSet,
@{ Name="ExpiryDate";Expression={ [datetime]::FromFileTime( $_."msDS-UserPasswordExpiryTimeComputed" ) } }
@iyre
iyre / New-HardwareReport.ps1
Last active September 23, 2022 19:31
Queries system properties using WMI and exports findings to an HTML file
<#
.SYNOPSIS
Generates a report of system configuration
.DESCRIPTION
Queries system properties using WMI and exports findings to an HTML file
.PARAMETER ComputerName
Specifies target. Defaults to local hostname
.PARAMETER FilePath
Specifies output path. Defaults to current location
.PARAMETER Open
@iyre
iyre / QueryCpuSpec.ps1
Created February 18, 2020 19:33
Query computers in domain for processor specifications
# duplicates indicate multiple processors
$Search = "OU=Domain servers,dc=contoso,dc=com"
Get-ADComputer -filter * -SearchBase $Search | Foreach-Object {
$property = "systemname","maxclockspeed","addressWidth","numberOfCores","NumberOfLogicalProcessors"
Get-WmiObject -computername $_.name -class win32_processor -Property $property -erroraction silentlycontinue | Select-Object -Property $property
}