Pocket Theories

Windows

Updated: September 8, 2024


winpty       
	  
git-bash is unable to run some Windows applications (Eg. Python), and winpty provides a compatibility layer.

Also see: StackOverflow

Powershell

Updated: September 19, 2024


# Create a folder
New-Item -Path 'C:\Games' -ItemType Directory

# Download a file with HTTP
Invoke-WebRequest 'http://a.a/a.txt' -OutFile C:\a.txt
# Can also use Invoke-RestMethod or
# (New-Object System.Net.WebClient).DownloadFile('http://a.a/a.txt', 'C:\a.txt')

# Async BITS transfer
Import-Module BitsTransfer
Start-BitsTransfer -Source 'http://a.a/a.txt' -Destination 'C:\a.txt'   # -Asynchronous

# Unset a variable
Remove-Variable -Name MyVar1
# or
Remove-Item -Path Variable:MyVar1

# Set a variable value to $null
Clear-Variable -Name MyVar1
# or
$MyVariable = $null

# Multi variable assignment
$a,$b,$c = 1, $true, "Nitin"

# Display output
Write-Output $a

# Strong-typing in PowerShell
[int]$number = 8
$number = "12345"  # The string is converted to an integer.
$number = "Hello"  # Error

# String interpolation
$a = "$($var1)"

# Copy file from remote
$theSrv = New-PSSession mySrv
Copy-Item -FromSession $theSrv C:\test1.txt -Destination C:\test.txt
# -ToSession is to "Copy file to remote"
# the local session where copying is initiated must have PowerShell 5 or higher installed.

# Copy-Item -Path \\mysrv\c$\test1.txt -Destination \\srv2\c$\test.txt;

# "New-PSDrive" is only visible in PowerShell; it's like "Net use y: \\ServerName\Share"
# New-PSDrive -Name Y -PSProvider filesystem -Root \\ServerName\Share
# Copy-Item BigFile Y:\BigFileCopy
# Remove with "Remove-PSDrive" like "net use drive-letter /delete"

# Web client can be used to copy to/from shared folders
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.NetworkCredential('user', 'pass')
$WebClient.DownloadFile('\\mysrv\s1\file1', 'file1.txt')
$WebClient.UploadFile('file1.txt', '\\mysrv\s1\file1')

# Allow file sharing in the firewall
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes

    

When using Invoke-Command, variables can be used in the ScriptBlock with the "$using:" prefix. See: https://powershellexplained.com/2016-08-28-PowerShell-variables-to-remote-commands/

See these instructions on enabling PowerShell remoting: https://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/