Powershell

Snippets

Admin check, just run Test-IsAdmin to call the function and output true or false. I use this for logging. If you want a hard fail for lack of admin use #Requires -RunAsAdministrator

function Test-IsAdmin {
    try {
        $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
        $principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
        return $principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator )
    } catch {
        throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_
    }
}

Start service on remote machine

Get-Service -ComputerName <machine> -Name <service> | Set-Service -Status Running

Grep

| findstr -i <term>

Connect to Sharepoint online

# the name appearing in the URL of your sharepoint site before sharepoint.com
$orgName=""
# connect without using get-credential so that the modern prompt is generated allowing 2FA
Connect-SPOService -Url https://$orgName-admin.sharepoint.com```

Dates

$Date = Get-Date -Format yyyy-MM-dd
$Year = Get-Date -Format yyyy
$Month = Get-Date -Format MM
$Day = Get-Date -Format dd
$Hour = Get-Date -Format HH
$Minute = Get-Date -Format mm
$Second = Get-Date -Format ss
$Time = Get-Date -Format HH-mm-ss
$TimeStamp = Get-Date -Format s | foreach {$_ -replace ":", "-"}

Storing and using credentials

Start with writing your password into a file

read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt 

Next use the file in a script

$userName = "FooRoo"
$passWord = cat C:\securestring.txt | convertto-securestring
$credEntial = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName,$passWord

<Command> -Credential $credEntial