Mastery of PowerShell

By Apr 20, 2016

Description:

Here we present the essential commands of the PowerShell and give some examples of using a few commands.

Preferencesoft

Essential commands

 

File and path manipulation

Cmdlet name

Command alias

Description

Get-ChildItem

gci, dir, ls

Lists current directory content

Get-Location

gl, pwd

Shows the current directory

Set-Location

sl, cd, chdir

Changes directory

tree

tree

Displays the directory structure

Push-Location

pushd

Saves the current directory on the stack and changes directory to the one given as argument

Pop-Location

popd

Pops the name on the stack and changes directory to it

Copy-Item

cpi, copy, cp

Deletes a folder with confirmation

Rename-Item

rni, ren

Renames an item

md

mkdir

Creates a new directory

Remove-Item

ri, del, erase, rmdir, rd, rm

Removes files or directories

Move-Item

mi, move, mv

Moves or renames files

Get-Content

Gc,type,cat

Displays file content on the stdout

New-Item

 ni

Creates a new item

 

System

Cmdlet name

Command alias

Description

Clear-Host

cls, clear

Clears the display

Get-Help

help, man

Gets help on a command or a topic

Set-Variable

set, sv

Sets the value of a variable

Get-PSDrive

gdr

Shows the drives

Write-Output

echo, write

Print to standard output

                      

Processes

Cmdlet name

Command alias

Description

Get-Process

gps, ps

Shows running processes

Stop-Process

spps, kill

Stop a running process

Measure-Command

Gets the execution time of a command or script

 

Strings

String comparison

Command

Description

-eq -ceq

Returns True or False according to whether the strings on both sides of the operator are equal. -ceq is case-sensitive

-ne -cne

Returns True or False according to whether the strings on both sides of the operator are different. -cne is case-sensitive

-ge -cge

Greater than or equal

-gt -cgt

Greater than

-lt -clt

Less than

-le -cle

Less than or equal

-Like -CLike

Wildcard comparison

-NotLike -CNotLike

 

-Match -CMatch

Regular expression comparison

-NotMatch -CNotMatch

 

String manipulation

Command

Description

string.TrimStart("string_to_remove")

Removes the specified string from the beginning of the string

string.TrimEnd("string_to_remove")

Removes the specified string from the end of the string

-replace

 

 

Escape characters in string

Escape character

Effect

`n

New line

`r

Carriage Return

`t

Tab

`a

Alert

`b

Backspace

`'

Single Quote

`"

Double Quote

``

Back quote

`0

Null

 

Control structures

Instruction

Description

if (condition) {
    #codeblock
}
 

Executes a block of code if a certain condition is True

if (condition) {
   #codeblock
}
elseif (condition) {
   #codeblock
}
else {
   #codeblock
}

Complete schemas of the if-elseif-else compound statement

$i = 0
while ($i -le 100) {
   echo $i
   $i++
}

Prints integers from 0 to 100

for ($i=0; $i -lt 10; $i++) {
   echo $i
}

Prints integers from 0 to 9

foreach ($i in $array) {
   #codeblock
}

Loops through all the objects in an array

continue

Skips the current loop iteration and begins the next

break

Exits the current loop iteration

|  >  >>  2>  2>>

 

function MyFunc {
   param ($p1)
   echo $p1
}

 

Declares a function with one parameter

 

Creating text files

Creating new files

Empty file

To create an empty file, you can use the command ni (New-Item):

ni FileName.ext

or
ni FileName.ext -ItemType "file" 

The created file is 0 bytes.

Text file

You can create an empty text file with the redirection operator:

$null > FileName.txt
or
$null | Out-File FileName.txt

The file subsequently created is in Unicode format. With Out-File you can create an empty file in another encoding.

$null | Out-File -Encoding "UTF8" FileName.txt

Showing file content

To get the contents of a file, we use the get-content command or one of its aliases gc, type, cat.

Suppose the file pwd.ini contains:
  • [passwords]
  • CESAMEopen
  • WalkInTheShadows
Type
gc pwd.ini
The output:
  • [passwords]
  • CESAMEopen
  • WalkInTheShadows

Showing n lines of a file

Consider the following file:

file1.txt:
  • line1
  • line2
  • line3
  • line4
  • line5
gc -totalcount 2 file1.txt
  • line1
  • line2

Merging text files

Consider the following two files:

file1.txt:
  • line11
  • line12
file2.txt:
  • line21
  • line22
  • line23
To merge the two files, we can copy the first file on file3.txt and use Add-Content:
 
copy file1.txt file3.txt
gc .\file2.txt | Add-Content .\file3.txt

It is possible to use the redirection operator >>, to merge files:

copy file1.txt file3.txt
gc .\file2.txt >> file3.txt

Warning! The redirection operator >> does not retain the encoding of the original file and works by default with Unicode.

Comparing file content

To compare two objects in PowerShell, use the command: Compare-Object with

Here is the full syntax:

compare-object -referenceobject $(get-content C:\test\testfile1.txt) -differenceobject $(get-content C:\test\testfile2.txt)
Compare (type file1.txt) (type file2.txt)

 


PowerShell

Categories

Share

Follow


KodFor Privacy Policy