Skip to content
Search

PowerShell:Basic

lastmod 2023-02-24created 2023-02-245 edits

PowerShell basics

Looking up commands

  • Get-Command - list commands
  • Get-Help - command usage. e.g. Get-Help dir
  • Get-Alias - list command aliases

Strings

Expandable string ("")

If a string contains a variable, the variable's value is inserted into the string.

$var1 = 5
$var2 = "$var1 - $var1"
$var2
# 출력값 : 5 - 5

Literal string ('')

Even if a string contains a variable, it is not replaced with the variable's value.

$var1 = 5
$var2 = '$var1 - $var1'
$var2
# 출력값 : $var1 - $var1

Escaping

PowerShell uses the backtick as its escape character.

Using the escape character keeps a variable name from being replaced with its value in an expandable string.

$var1 = 5
$var2 = "`$var1 = $var1"
$var2
# 출력값 : $var1 = 5

Replace

$str = 'abc_abc'
$$ = 'xyz'
$1 = '123'

$str -replace 'a(\w)', '$1' #  bc_bc - a를 제외한 키워드 출력
$str -replace 'a(\w)', "$1" # 123c_123c - 변수 해석
$str -replace 'a(\w)', '$$$1' # $bc_$bc
$str -replace 'a(\w)', "$$$1" # xyz123c_xyz123c

Conditionals

if - elseif - else

$var = 5

if ( $var -eq 0 )
{
   echo "`$var = 0"
}
elseif ($var -eq 1) {
   echo "`$var = 1"
}
else {
   echo "`$var = $var"
}
# 출력값 : $var = 5

switch (multi-way conditional)

$a = 5
switch($a)
{
    1 { '$a = 1' }
    2 { '$a = 2' }
    3 { '$a = 3' }
    4 { '$a = 4' }
    default { "etc"}
}

Comparison operators

Operator

Meaning

Symbol

-eq

Equals

=

-ne

Does not equal

!=

-gt

Greather than

>

-lt

Less than

<

-ge

Greater than or equal to

>=

-le

Less than or equal to

<=

Wildcardcomparison

  • -like: matches.
  • -notlike: does not match.
$var= "Good morning"
$var -like "*morning"
# 출력값 : true

Regularexpression comparison

  • -match: regular expression match
  • -notmatch: negated regular expression match
$var= "Good morning"
$var -match "morning"
# 출력값 : true

Loops

while

$i= 0
while($i -lt 3)
{
    "`$i = $i"
    $i++
}

For

For($i = 0; $i -lt 3; $i++)
{
   '$i = ' + $i
}

Foreach

A loop that iterates once for every item in an array or collection. (Unlike For, it handles the counting internally)

$array = 1..3     # 1..3은 1,2,3을 갖고있는 int 배열을 만든다.
foreach($a in $array)
{
   $a
}

Or:

$array = 1..3
$array | ForEach-Object { $_ }

$_ refers to each element of the array or collection passed through the pipeline.

Arrays

Creating an empty array

$array = @()

Adding values

$array += 1
$array += 2

Using commas

$sArray = "aa","bb","cc"
$nArray = 1,2,3,4,5

Using the range operator

$nArray = 1..5

Delimited strings

$s = "aa,bb,cc,dd"
$array = $s.split(",")

Getting the array size

$array.Count

Reading array values

for($i=0;$i -lt $array.Count; $i++)
{
   $array[$i]
}

Or

foreach($item in $array)
{
   $item
}

Functions

Definition

function add_func {
Param ([int]$parm1 , [int]$parm2)

    return $parm1+$parm2
}

$result = add_func -parm1 10 -parm2 5

Or

function func
{
    echo $args[0]
    echo $args[1]
}

func Hello Powershell

Function arguments and script file arguments are stored in the $args variable.

See also

Favorite site

author Zer0 sitemap