Set Environment Variable

[Environment]::SetEnvironmentVariable("PATH", "C:\Tools;$env:PATH", "User")

Wget with proxy authentication

[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
wget https://updates.jenkins-ci.org/download/war/2.138.1/jenkins.war

Source: https://stackoverflow.com/questions/14263359/access-web-using-powershell-and-proxy

Add username and password to https url

Adds $username:$password between https:// and the rest of the url

$username = "user"
$password = "pass123"
$giturl = "https://scm.sc.intra"

$url = $giturl -split "\/\/"

echo "$($url[0])://${username}:${password}@$($url[1])" 

Security Question in Script

$Confirm = Read-Host "Force (y/N)"
if ($Confirm -Match "^y$") {
    $gitPushCommand = "Force"
} else {
    $gitPushCommand = "Not force"
}
Write-Host "Command: $gitPushCommand"

Calculate Folder Size

{0:N2} MB" -f ((Get-ChildItem C:\users\ -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)

Source: https://www.gngrninja.com/script-ninja/2016/5/24/powershell-calculating-folder-sizes

Usage of parameters

[CmdletBinding(DefaultParametersetName='None')]
Param (
    [Parameter(
        Mandatory=$false, 
        HelpMessage="Enter target path")]
    [ValidatePattern("[c-d]\:\\[0-9a-z]*")]
    [string] $Basedir = "D:\Dev",

    [Parameter(
        Mandatory=$false, 
        HelpMessage="Enter source location for sw packages")]
    [string] $baseurl= "https://myserver/tools",

    [parameter(
        ParameterSetName="SilentInstall",
        Mandatory=$false,
        HelpMessage="Component to be installed, use 'all' to install all, use -Show to show all components")]
        [Switch]$Silent,

    [parameter(
        Mandatory=$false,
        HelpMessage="Show all installable components")]
        [Switch]$Show,

    [parameter(ParameterSetName="SilentInstall", Mandatory=$true)]
    [string] $component
)

Resize a partition

$part=Get-Partition
$size=Get-PartitionSupportedSize -InputObject $part
Write-Host "Resizing partition to size: $($size.SizeMax)"
Resize-Partition -InputObject $part -Size $size.SizeMax

Powershell Credentials

#interactive
$mycredentials = Get-Credential

# When you have to provide credentials in non-interactive mode, you can create a PSCredential object in the following way.
# You can now pass ```$mycreds``` to any ```-PSCredential``` input parameter
$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)

Source: https://blogs.msdn.microsoft.com/koteshb/2010/02/12/powershell-how-to-create-a-pscredential-object/

Show Certificates

Display Certificates from Certificates store in Windows

Set-Location Cert:\CurrentUser\My
Get-ChildItem | Format-Table Subject, FriendlyName, Thumbprint -AutoSize

Invoke-Expression Advanced

Advanced Invoke-Expression which catches errors

$isContinue = $true
while ($isContinue) {
    Push-Location $workDir
    try {
        Write-Host $(Get-Location)
        Invoke-Expression $command 2>&1
        $isContinue = $false
    } catch {
        $msg = $($_.Exception.Message)
        Write-Host "### [ERROR] $msg"
        # abort on critical issues, otherwise continue
        if (!($msg -match "Found possible branch point")) {
            return
        }
    } finally {
        Pop-Location
    }
}

Invoke-WebRequest with username and password for basic authentication

<# 
https://stackoverflow.com/questions/27951561/use-invoke-webrequest-with-a-username-and-password-for-basic-authentication-on-t?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
Invoke-WebRequest -Uri $url -Credential $cred
#>

Curl-With-Auth 
$user = "shaunluttin"
$pass = "super-strong-alpha-numeric-symbolic-long-password"
$pair = "${user}:${pass}"
Encode the string to the RFC2045-MIME variant of Base64, except not limited to 76 char/line.

$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
Create the Auth value as the method, a space, and then the encoded pair Method Base64String

$basicAuthValue = "Basic $base64"
Create the header Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

$headers = @{ Authorization = $basicAuthValue }
Invoke the web-request

Invoke-WebRequest -uri "https://api.github.com/user" -Headers $headers

Source: https://stackoverflow.com/questions/27951561/use-invoke-webrequest-with-a-username-and-password-for-basic-authentication-on-t?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa