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,...
Get External IP Address
Invoke-RestMethod 'https://api.ipify.org?format=json' | Select-Object -ExpandProperty IP
Zip All Files in Folder
# Get the files to zip $FilesToZip = Get-ChildItem -Path $FolderPath -File # Load the assembly to get the zip functionality [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) | Out-Null # Set compression level $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal # Set Zip file pathc based on folder $ZipPath = Join-Path $FolderPath "$(Split-Path $FolderPath -Leaf).zip" # initialize the zip file $Archive = [System.IO.Compression.ZipFile]::Open( $ZipPath, "Update" ) #...
Yes/No Prompt
$yes = new-Object System.Management.Automation.Host.ChoiceDescription "&Yes","help" $no = new-Object System.Management.Automation.Host.ChoiceDescription "&No","help" $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no) $answer = $host.ui.PromptForChoice("Prompt","Click yes to continue",$choices,0) if($answer -eq 0){ Write-Host "You clicked Yes" }elseif($answer -eq 1){ Write-Host "You clicked No" }