Get AD Users by Last Logon Date
Get-ADUser -Filter * -SearchBase "CN=Users,DC=contoso,DC=com" -ResultPageSize 0 -Property CN, LastLogonTimestamp | Select-Object -Property CN, SamAccountName, @{ n = "LastLogonDate"; e = { [datetime]::FromFileTime( $_.lastLogonTimestamp ) } } | Sort-Object -Property LastLogonDate
PowerShell Timer with Alarm
Function Start-Timer { param( [Parameter(Mandatory=$true)] [int]$seconds, [Parameter(Mandatory=$false)] [switch]$alarm ) $a = $(Get-Date) For($i=1;$i -le $seconds;$i++){ Write-Progress -Activity "$seconds Second Timer" -Status $i -PercentComplete $(($i/$seconds)*100) -id 1 Start-Sleep -Seconds 1 } if($alarm){ For($i=1;$i -le 10;$i++){ [console]::beep(500,300) } } }
Check if Port is Open
$socket = new-object Net.Sockets.TcpClient $socket.Connect($IPAddress,$Port) $socket.Connected
Get User’s Desktop Path
$ShellFolders = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' $ShellFolders.Desktop
Easily Switch Between Azure Subscriptions and Tenants
$SubscriptionId = "Your-Subscription-Guid" if($(Get-AzureRmContext).Subscription.SubscriptionId -ne $SubscriptionId){ Set-AzureRmContext -SubscriptionId $SubscriptionId -ErrorAction SilentlyContinue if($(Get-AzureRmContext).Subscription.SubscriptionId -ne $SubscriptionId){ Clear-AzureRMContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue Clear-AzureRmDefault -Force -ErrorAction SilentlyContinue $connect = Add-AzureRmAccount -SubscriptionId $SubscriptionId } }
Make Your Computer Talk
Add-Type -AssemblyName System.Speech $Speech = New-Object System.Speech.Synthesis.SpeechSynthesizer $Speech.Speak("I'm sorry Dave, I'm afraid I can't do that")
First Weekday of Month
# Get first day of Month $FirstWeekDay = Get-Date $date.Date -day 1 # if day is Sat or Sun add days until it is not while(0,6 -contains $FirstWeekDay.DayOfWeek.value__){ $FirstWeekDay = $FirstWeekDay.AddDays(1) } $FirstWeekDay
Last Weekday of Month
# Get last day of Month $LastWeekDay = (Get-Date $date.Date -day 1).AddMonths(1).AddMilliseconds(-1) # if day is Sat or Sun subtract days until it is not while(0,6 -contains $LastWeekDay.DayOfWeek.value__){ $LastWeekDay = $LastWeekDay.AddDays(-1) } $LastWeekDay
Input Box Pop-up
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null $Value = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a value", "Title", $null)
Get the AD Groups for a User with 1 Level of Inheritance
# Get the direct group memberships $UserGroups = Get-ADPrincipalGroupMembership $UserName | Select distinguishedName, GroupCategory, GroupScope, name, objectClass, objectGUID, SamAccountName, SID, @{l='Membership';e={'Direct'}}, @{l='Parent';e={$null}} # Get the group membership 1 level down foreach($group in $UserGroups){ $UserGroups += Get-ADPrincipalGroupMembership -Identity $group.distinguishedName | Select distinguishedName, GroupCategory, GroupScope, name, objectClass, objectGUID, SamAccountName, SID, @{l='Membership';e={'Inherit'}}, @{l='Parent';e={$group.distinguishedName}} } # Display results $UserGroups | FT name, GroupCategory, GroupScope,...
