Skip to content

Instantly share code, notes, and snippets.

@jessewolcott
jessewolcott / ADShuffle.ps1
Created October 30, 2023 15:22
ADShuffle.ps1
$Active_OU = "OU=Systems,DC=fabrikam,DC=com"
$Stale_OU = "OU=DisabledOU,DC=fabrikam,DC=com"
$Log_Path = "$PSScriptRoot"
$ErrorActionPreference = 'Stop'
$Lookback = '30'
try {
# Check if OUs Exist
Get-ADOrganizationalUnit -Identity $Active_OU |Out-Null
Get-ADOrganizationalUnit -Identity $Stale_OU |Out-null
@jessewolcott
jessewolcott / ExchangeAndADLogins.ps1
Created August 21, 2023 21:08
Find Last Exchange and AD Login!
$Emails = @(
"test@contoso.com"
"Otherguy@fabrikam.com"
)
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
$Results = foreach ($Email in $Emails){
$LLT = (Get-MailboxStatistics -Identity $Email).LastLogonTime
$ADLLT = (Get-ADUser -Filter {UserPrincipalName -eq $Email} -ErrorAction SilentlyContinue -Properties *)
$Version = [System.Version](Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{066E438B-355E-4A79-B1C2-2D1C71CF000C}" -ErrorAction SilentlyContinue).displayVersion
If ($null -ne $Version){
$ProgramVersion_target = [System.Version]'8.0.0.65'
if($Version -ge $ProgramVersion_target){
Write-Host "Found it!"
}
}
$ProductName = "XM Fax CE 23.1"
$UninstallStrings = @("HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall","HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |`
foreach-object{Get-ChildItem -Path $_ |`
Get-ItemProperty |`
Select-Object DisplayName,UninstallString
}|`
Sort-Object |`
Where-Object {($_.DisplayName) -and ($_.DisplayName -match $ProductName)} |`
Sort-Object Displayname |`
Select-Object -ExpandProperty UninstallString
$ScheduledTaskName = "Scriptname"
$Time = "00:00"
$Argument = ("-executionpolicy bypass -file 'C:\Scripts\Somescript.ps1'")
$TaskParams = @{
Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "$Argument"
Trigger = New-ScheduledTaskTrigger -Daily -At $Time
Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -RunOnlyIfNetworkAvailable
Principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest
Description = "Run $($ScheduledTaskName) at $Time daily"
@jessewolcott
jessewolcott / OU_to_group.ps1
Last active May 18, 2023 20:56
OU to Group
$Full_OU_Path = "" # Example: Full OU path. Right click in ADUC and go to object! "contoso.com/Systems/Windows10"
$NewGroup = "" # Example: Group name, displayname should work, but SAM would probably be preferrable. "New York Office"
Write-Output "Getting OU Path from Canonical name"
$OU_Path = (Get-ADOrganizationalUnit -Filter * -Properties CanonicalName,Name,DistinguishedName | Where-Object -FilterScript {$_.CanonicalName -like "*$Full_OU_Path"} | Select-Object -ExpandProperty DistinguishedName)
Write-Output "Found $OU_Path"
Write-Output "Getting Users from $NewGroup"
$Users = Get-ADUser -Filter * -SearchBase $OU_Path
Write-Output ("Found "+($users.count)+" Users")
$AppId = ""
$Client_Secret = ""
$TokenURL = "" # Example: https://www.example.com/oauth2/token
$Hostname = "" # Example: https://www.example.com/
$RequestBasePath = "" # Example: api/config/v1
$APIPath = "" # Example: /users
$Body = @{
client_id = $AppId
client_secret = $Client_Secret
$LocalImgFolder = "Path\To\jessewolcott.github.io\assets\img"
$Years = (Get-Childitem -Path $LocalImgFolder).Fullname
$EmptyFolders = foreach ($Year in $Years){
Remove-Item -Path ((Get-ChildItem -Path $Year -Recurse | Where-Object {($_.PSIsContainer -eq $True) -and ($_.GetFiles().Count -eq 0)}).FullName)
}
@jessewolcott
jessewolcott / ThumbnailCleanup.ps1
Created April 15, 2023 17:57
Cleanup Wordpress Thumbnails
$LocalImgFolder = "Path\To\jessewolcott.github.io\assets\img"
$Years = (Get-Childitem -Path $LocalImgFolder).Fullname
$ThumbNails = foreach ($Year in $Years){
(Get-ChildItem -Path $Year -Recurse -Filter "*-*x*").FullName
}
foreach ($Thumbnail in $Thumbnails){ Remove-Item -Path $Thumbnail}