Introduction to Powershell syntax summary

Posted by Aeolus on Fri, 26 Nov 2021 08:14:22 +0100

Windows PowerShell is a command-line shell and scripting environment that enables command-line users and scripters to take advantage of the power of the. NET Framework. You can simply understand that Powershell is a more advanced cmd under Window. Here are some basic introduction syntax of Powershell. There are still many applications of Powershell, and advanced usage needs to be deeply studied.

Know PowerShell

$psversiontable view version

The power of PowerShell

  1. Fast and convenient
  2. object-oriented
  3. Support compatibility with. net vbs, etc
  4. Extensibility

PowerShell shortcuts

alt+f7 Clear command history
esc Clear current command line

PowerShell executes external commands

&"notepad"

PowerShell command set

Naming commands with Gerunds

  • cmd command can be used directly in powershell

Get command # get a list of all commands

  • Help command the world

get-help

PowerShell alias usage

Get alias - name ls # query the original command of ls

PowerShell custom alias

Set alias - name pad - value notepad # set temporary alias, assign notepad to pad alias del alias:pad # delete alias export alias 1.psl # export alias import alias - force 1.psl # force import alias

PowerShell variable base

  • Equal sign assignment

$name="xx"

  • Special variable names are enclosed in curly braces

${"asdsad asdsd" var ()}

PowerShell variable operation

  • Support multivariable assignment
name,

name2=1,2

  • View variables in use

Get variable num * # find num specific variable value

  • Determines whether the variable exists

Test path variable: num1 # returns a Boolean value

  • Delete variable name

Remove-Variable num1

PowerShell automation variables

Common variables

  • $pid
  • $home

PowerShell environment variables

ls env: # view current environment variables

$env:os # outputs the value of a key

$env:0s="Linux" # temporary assignment variable

  • Set permanent environment variable (. net mode)

[environment]::setenvironmentvariable("PATH","D:","User")

PowerShell script execution policy

Get executionpolicy # view the current running policy

Policy classification
> get-help set-executionpolicy

grammar
    Set-ExecutionPolicy [-ExecutionPolicy] {Unrestricted | RemoteSigned | AllSigned | Restricted | Default | Bypass | U
    ndefined} [[-Scope] {Process | CurrentUser | LocalMachine | UserPolicy | MachinePolicy}] [-Confirm] [-Force] [-What
    If] [<CommonParameters>]

Set executionpolicy remotesigned # sets the policy that can be run

Call between PowerShell and other script programs

  1. powershell can execute bat, vbs and psl files directly
  2. bat calls powershell using powershell & "file path"

PowerShell conditional operator

  • -eq # equals
  • -lt # less than
  • -gt # greater than
  • -contains # does not contain

1,3,5 -contains 3

  • -notcontains
  • -not
  • -and
  • -or
  • -ne # is not equal to

1,3,5 -ne 3

PowerShell condition judgment [if statement]

if(

num -gt 100){"1"} elseif(

num -eq 100){"0"} else {"-1"}

PowerShell condition judgment [switch statement]

$number = 49
switch($number)
{
    {$_ -le 50} {"This value is less than 50"}
    {$_ -eq 50} {"This value is equal to 50"}
    {$_ -gt 50} {"This value is greater than 50"}
}
\\ $_Representative variable

PowerShell loop structure [foreach statement]

$arr = 1,2,3,4,5 perhaps $arr=1..10
foreach ($n in $arr)
{
    if($n -gt 5)
        {
            $n
        }
}

PowerShell loop structure [while statement]

$num = 15
while($num -gt 15)
{
    $num
    $num=$num-1
}
  • dowhile runs at least once
do
{
    $num
    $num=$num-1
}
while($num -gt 15)

Use of break and continue keywords

break out

$num=1
while($num -lt 6)
{
    if($num -gt 4)
    {
        break
    }
    else
    {
        $num
        $num++
    }
}

continue skip

$num=1
while($num -lt 6)
{
    if($num -gt 4)
    {
        break
    }
    else
    {
        $num
        $num++
    }
}

PowerShell loop structure [for statement]

$sum=0
for($i=1;$i -;e 100;$i++)
{
    $sum=$sum+$i
}
$sum
\\ Calculate the sum of the first 100 numbers

PowerShell loop structure [switch loop]

$num=1..10
swith($num)
{
    {($_ % 2) -eq 0} {"$_ It's an even number"}
    {($_ % 2) -ne 0} {"$_ It's an odd number"}
}

Creation of PowerShell array

$arr = ipconfig
$arr
$arr -is [array]
\\ Determine whether it is an array

The arr=ipconfig #cmd command can also be executed

Arr = @ () # create empty array $arr = 1.. 10, "string", (get date) # create mixed array

PowerShell access array

$arr[0..2]

PowerShell custom functions and calls

function myping($url)
{
    ping $url
}
myping www.baidu.com

PowerShell function return value

return

PowerShell definition text

`Escape

PowerShell enables user interaction

$input=read-host "Please enter a specific path"
"Your current path is:$input"

PowerShell format string

"my name is {0} ,iam {1} years old" -f $name,$age