|
# Get current directory of PowerShell script being executed |
|
$MyInvocation.MyCommand.Path |
|
(Get-Item -Path ".\" -Verbose).FullName |
|
|
|
# Get Powershell module version |
|
(Get-Module Azure).Version |
|
|
|
# Get user's 'Documents' folder |
|
[environment]::getfolderpath("mydocuments") |
|
|
|
# Convert UTF-8 string to base 64 |
|
$bytes = [System.Text.Encoding]::UTF8.GetBytes("Hello World") |
|
$base64String = [System.Convert]::ToBase64String($bytes) |
|
# Convert base64 to UTF-8 string |
|
$bytes = [System.Convert]::FromBase64String("SGVsbG8gV29ybGQ=") |
|
$utf8String = [System.Text.Encoding]::UTF8.GetString($bytes) |
|
|
|
# Add property to a CustomPSObject |
|
Add-Member -InputObject $myPSObject -MemberType NoteProperty -Name "propertyName" -Value "propertyValue" |
|
|
|
# List all files recursively in a directory |
|
ls -Path C:\Myfolder -Recurse |
|
|
|
# Get all environment variables |
|
ls env: |
|
|
|
# List environment variables in an array |
|
$env:Path -split ';' |
|
$env:Path.split(";") |
|
|
|
# Set environment variable |
|
$env:JAVA_HOME="C:\Program Files\Java\jdk1.8.0_121" |
|
|
|
# Get environment variable ProgramFiles |
|
ls env:ProgramFiles |
|
|
|
# Stop a Windows process by it's process name |
|
Get-Process -name Notepad | Stop-Process |
|
|
|
# Get environment variable ProgramFiles(x86) |
|
# ls env:ProgramFiles(x86) won't work |
|
dir env:ProgramFiles`(x86`) |
|
dir "env:ProgramFiles(x86)" |
|
${Env:ProgramFiles(x86)} |
|
[Environment]::GetEnvironmentVariable("ProgramFiles(x86)") |
|
|
|
# UNC path to a VM in the Windows network |
|
\\server-name\file_path |
|
# UNC path of C:\system32 of a VM in the Windows network |
|
\\server-name\c$\system32 |
|
|
|
<# |
|
.SYNOPSIS |
|
Gets file encoding. |
|
.DESCRIPTION |
|
The Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM). |
|
Based on port of C# code from http://www.west-wind.com/Weblog/posts/197245.aspx |
|
.EXAMPLE |
|
Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} |
|
This command gets ps1 files in current directory where encoding is not ASCII |
|
.EXAMPLE |
|
Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} | foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII} |
|
Same as previous example but fixes encoding using set-content |
|
#> |
|
function Get-FileEncoding |
|
{ |
|
[CmdletBinding()] Param ( |
|
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string]$Path |
|
) |
|
|
|
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path |
|
|
|
if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) |
|
{ Write-Output 'UTF8-BOM' } |
|
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) |
|
{ Write-Output 'Unicode' } |
|
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) |
|
{ Write-Output 'UTF32' } |
|
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) |
|
{ Write-Output 'UTF7'} |
|
else |
|
{ Write-Output 'ASCII' } |
|
} |