Notes

Working notes on Notes: Execution-Policy, Status, ExecutionPolicy, Enable, Revert, and Execute Script.

UjnotesCC0 1.0

Working notes on Notes: Execution-Policy, Status, ExecutionPolicy, Enable, Revert, and Execute Script.

PowerShell.com.

Execution-Policy

Status

ExecutionPolicy

Restricted.

RemoteSigned.

Enable

set-executionpolicy remotesigned

Revert

set-executionpolicy restricted

Execute Script

Requires absolute path. else

.\<>.ps1

Hide Runtime Window.

PowerShell.exe -windowstyle hidden { your script.. }

Version

$PSVersionTable.

Debug

$var

Value in console.

Write-Host not required (?).

Array

$array =.

Array (<element_1>, <element_2> … )

{ … } hash table.

Multi Dimensional array.

@( @() ).

{ <key>=”<value>”, … }

Select Elements

array[0 .. n]

0 - n elements.

array[i, j, k]

Selective.

this can be joined

-join ‘’

Function

function fn ($a, $b …) {

….

}.

Fn A B.

() and , separated in .net part.

Echo

Write-Host

Escape $

$

`$.

"X"

\"X\".

Perhaps ‘\’ becomes special if it is succeeded by a ‘`’.

\

Need not be escaped unless its ADO path.

`0

Null.

`a

Alert bell/beep.

`b

Backspace.

`f

Form feed.

`n

New line.

`r

Carriage return.

`t

Horizontal tab.

`v

Vertical tab.

`'

Single quote.

`"

Double quote.

Getch

$_ = $host.UI.RawUI.ReadKey("NoEcho”, “IncludeKeyDown").

Read-Host

Read-Host "Enter a Password" -assecurestring

This command displays the string "Enter a Password:" as a prompt. As a value is being entered, asterisks (*) appear on the console in place of the input. When the Enter key is pressed, the value is stored as a SecureString object in the $pwd_secure_string variable.

Concatenate Strings

$A$B

For param passing.

“xyz$var1”

+ worked in .net part.

+= can be used.

[-f operator].

> "comp {0} has {1} MB of memory." -f "L001", "4096"

Comp L001 has 4096 MB of memory.

Join operator

Join operator , list - join ‘<filler>’

Eg. ‘x’, ‘y’, ‘z’ - join ‘’ => xyz‘.

Split String

split(" ", [StringSplitOptions]'RemoveEmptyEntries')

Remove empty entries.

split ‘ {2,}’

RegEx Matches 2 or more Spaces as delimiter.

Split ‘ +’ | Split ‘[,]+}.

Multiple.

Terminate Script

Exit.

Test file, directory, registry

Test-Path.

Remove

Remove-Item

Get Time.

(Get-Item C:\Windows\notepad.exe).LastWriteTime.

.toString().

Operators

-not        Not

!

-and        And
-and -or        Or
-eq        Equal to
-eq -lt        Less than
-lt -gt        Greater than
-gt -ge        Greater than or Equal to
-ge -le        Less than or equal to
-le -ne        Not equal to

True or False

if ($var -eq $TRUE)

Case insensitive.

Var = $TRUE;.

If ElseIf Else.

Must have { } body.

Comment

Comment <#

….

#.

#>.

Include

. “path.ps1”.

Modules

http://msdn.microsoft.com/en-us/library/dd878310%28v=vs.85%29.aspx

Copy

Copy -recurse

Only container dir.

  • wildcard can be used at i.

Copy-Item i o_Dir.

Paths

Current-Dir must be explicitly stated with .\.

Can have ‘\’ ‘\.\’ ‘\..\’--will be deduced.

Create

Create New-Item -ItemType directory -Path <path>

Import

import-csv c:\temp\customers.csv

Rename

Rename-Item.

Ren | RnI.

Get-Content c:\scripts\test.txt

GC Type Cat.

[A new-line separated automatically builds an an array].

-totalcount n

Number of lines.

-totalcount -last n

Last n lines.

Measure-Object

Get Count.

[System.IO.File]::OpenText($filename).

Current Directory

$(get-location)

$pwd>.

Resolve Path

[IO.Path]::GetFullPath().

resolve-path ..\frag

Tee

Tee -OutVariable

Also allows appending to the variable.

To stop relay to output.

| Out-Null.

Tee.

Duration

TotalMilliseconds

Time from beginning - PERHAPS.

Directory Tree

$fileEntries = [IO.Directory]::GetFiles("c:\scripts");.

Foreach($fileName in $fileEntries).

{

[Console]::WriteLine($fileName);.

}.

$fileName

  • has path glued at the beginning.

PS C:\> dir | where {$_.PsIsContainer} | Select-Object Name.

Name.

—-.

Projects.

Cygwin.

Documents and Settings.

Convert to UTF-8

$MyFile = Get-Content $MyPath.

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False).

[System.IO.File]::WriteAllLines($MyPath, $MyFile, $Utf8NoBomEncoding)

[System.IO.File]::WriteAllLines($MyPath, $MyFile) -equivalent

Read file into Array

$lines=file('file.txt');.

2nd Param - Flags ( | ORable).

FILE_USE_INCLUDE_PATH.

Search for the file in the include_path.

FILE_IGNORE_NEW_LINES.

Do not add newline at the end of each array element.

FILE_SKIP_EMPTY_LINES.

Skip empty lines.

Place holder variable

$_

| fn ( $_ ).

? correct syntax.

NULL

$NULL.

Undefined variables, like array element outside bound equals this.

Can be used in comparision.

Out file encoding

Out file encoding | out-file -encoding ASCII <out-file>

Encoded to UTF-8.

Need to test with non ASCII characters.

Updated: 2026 Aug 19