## Create a session$s=New-PSSession<computer-name>## Run command(s) on the removeInvoke-Command-Session$s{<command(s)torun>}## Close the remote sessionRemove-PSSession$s
# Create a session$Session=New-PSSession-ComputerName"Server01"-Credential"Contoso\User01"# Copy item from remote $session to local -Destination)Copy-Item"C:\MyRemoteData\test.log"-Destination"D:\MyLocalData\"-FromSession$Session
On Linux, where everything is better and easier, you just run uptime to get a machine's uptime. On Windows, you have to do extra stuff because... Powershell...
On laptops or devices with portable power, you can generate a battery report with the following command (find the report at the path you put after /output):
Write-Host"This is the first part of a long string, with no formatting."-NoNewline;Write-Host"This part of the string will appear inline (on the same line) as the previous string,"-NoNewline;Write-Host"and can even be broken up mid-sentence! Check the source code to see this in action."-NoNewline;Write-Host""-NoNewline;Write-Host"And I'm purple, just because"-ForegroundColorpurple-NoNewline;Write-Host"Ok that's all."
Set/Unset environment variables
Warning
You must be in an elevated/administrative prompt for these commands.
functionSet-EnvVar{<# Set an environment variable. If -Target Machine or -Target User, the env variable will persist between sessions. Usage: Set-EnvVar -Name <name> -Value <value> Set-EnvVar -Name <name> -Value <value> -Target Machine Params: Name: The name of the environment variable Value: The value of the environment variable Target: The scope of the environment variable. Machine, User, or Process Example: Set-EnvVar -Name "EXAMPLE_VAR" -Value "example value" Write-Host $env:EXAMPLE_VAR #>param([string]$Name,[string]$Value,[ValidateSet('Machine','User','Process')][string]$Target='User')Write-Host"Setting [$Target] environment variable "$Name"."If($Target-eq'Process'){Write-Warning"Environment variable [$Target] will not persist between sessions."}else{Write-Information"Environment variable [$Target] will persist between sessions."}try{[System.Environment]::SetEnvironmentVariable($Name,$Value,[System.EnvironmentVariableTarget]::$Target)}catch{Write-Error"Unhandled exception setting environment variable. Details: $($_.Exception.Message)"}}
functionRemove-EnvVar{<# Remove/unset an environment variable. Usage: Remove-EnvVar -Name <name> Remove-EnvVar -Name <name> -Target Machine Params: Name: The name of the environment variable Target: The scope of the environment variable. Machine, User, or Process Example: Remove-EnvVar -Name "EXAMPLE_VAR" Write-Host $env:EXAMPLE_VAR #>param([string]$Name,[ValidateSet('Machine','User','Process')][string]$Target='User')try{[System.Environment]::SetEnvironmentVariable($Name,$null,[System.EnvironmentVariableTarget]::$Target)}catch{Write-Error"Unhandled exception removing environment variable. Details: $($_.Exception.Message)"}}
$Site="https://www.google.com"while($true){try{## Make HTTP HEAD request$response=Invoke-WebRequest-Uri"$($Site)"-MethodHead## Output HTTP status codeWrite-Output"$(Get-Date) Ping site '$($Site)': [$($response.StatusCode): $($response.StatusDescription)]"}catch{Write-Error"$(Get-Date): Request failed. Error: $($_.Exception.Message)"}## Pause for $RequestSleep secondsStart-Sleep-Seconds5}
functionGet-HTTPSiteAvailable{Param([string]$Site="https://www.google.com",[string]$RequestSleep=5)while($true){try{## Make HTTP HEAD request$response=Invoke-WebRequest-Uri"$($Site)"-MethodHead## Output HTTP status codeWrite-Output"$(Get-Date) Ping site '$($Site)': [$($response.StatusCode): $($response.StatusDescription)]"}catch{Write-Error"$(Get-Date): Request failed. Error: $($_.Exception.Message)"}## Pause for $RequestSleep secondsStart-Sleep-Seconds$RequestSleep}}
Open a list of URLs
This script iterates over an array of URL strings and opens them in your default browser.
## Declare an array of URL strings$Links=@("https://example.com","https://example.com/example","https://example.com/test")## Iterate over URLs and open them$Links|ForEach-Object{Write-Output"Opening URL: $_"Start-Process"$($_)"}
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe-command"(Add-Type -MemberDefinition '[DllImport(\"user32.dll\")] public static extern int PostMessage(int a, int b, int c, int d);' -Name f -PassThru)::PostMessage(-1, 0x112, 0xF170, 2)"