Skip to main content

One Liners

#Delete a folder under the $log Container on the Azure Blob Storage
Get-AzStorageBlob -Container `$log -Blob "Myfolder*" -Context $Context | Remove-AzStorageBlob -Verbose

#Get AD user whose SamAccountName and MailNickname are not the same
Get-AdUser -Filter {Mailnickname -gt 0} -Properties MailNickName | Where {$_.SamAccountName -ne $_.MailNickName}

#Get Azure AD User and their devices
Get-AzureADUser -All:$true -PipelineVariable PV | Get-AzureADUserOwnedDevice | Select @{n='UPN';e={$PV.UserPrincipalName}},DisplayName

#Grab HKLM hive to a text file
Get-ChildItem HKLM:\ -Recurse -ErrorAction Ignore | Foreach-Object name > HKLM-snapshot.txt

#Get all user with 'ENTERPRISEPACK' License Assigned
Get-MgUser -Filter "assignedLicenses/any(x:x/skuId eq $((Get-MgSubscribedSku).Where{$_.SkuPartNumber -eq 'ENTERPRISEPACK'}.SkuId))" -All

Useful If statements

#If the folder exists
If (Test-Path -Path 'C:\temp\MyFolder' -PathType Container){$true}

#If the file exists
If (Test-Path -Path 'C:\temp\test.txt' -PathType Leaf){$true}

#If the file last update time is within 5 days
If ($file.LastWriteTime -gt (Get-Date).AddDays(-5)){$true}

#If the file has some content
If ($file.length -gt 0){$true}
If (Get-Content $file){$true}

#If the Path is Absolute Path (not relative path)
If ([System.IO.Path]::IsPathRooted($Path)){$true}

#If the host is replying to ping request
If (Test-Connection -ComputerName PC01 -Count 2 -Quiet){$true}

#If the service (Bits) is running
If((Get-Service 'Bits').Status -eq "Running"){$true}

#If the website is running okay
If ((Invoke-WebRequest "https://pscode.dev").StatusCode -eq 200){$true}

#If the $address variable is a upn/email address
If ([Bool]($address -as [System.Net.Mail.MailAddress])){$true}

#Continue? yes or no
$Answer = Read-Host "Continue (y/n)?"
If($Answer.ToLower() -eq 'n'){break}