Check sAMAccountName Requirements
Function Check-sAMAccountName { [CmdletBinding()] [OutputType([string])] param( [parameter(Mandatory=$true)] [string]$ScriptParameters ) # exclude the characters " * + , / : ; < = > ? @ [ \ ] | $excludedChars = 34,42,43,44,47,58,59,60,61,62,63,64,91,92,93,124 $StringBuilder = New-Object System.Text.StringBuilder # split name into Char Array and check each character $sAMAccountName.ToCharArray() | ForEach-Object{ try{ # convert char to ascii decimal $ascii = [byte][char]$_ #...
Format Data Returned from Get-PnPListItem
If you have ever used the SharePoint PnP PowerShell Cmdlets you know that the data returned from the list is not done in the cleanest manor. It is returned as a hashtable and it includes all the internal columns. So, I created a function that will convert this hashtable to a standard PowerShell object and only return the columns you...
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