`

Windows PowerShell Language Quick Reference

阅读更多

Windows PowerShell Language Quick Reference

 

Native Support for Different Type Systems

Windows PowerShell adapts WMI, XML, ASDI, <city w:st="on"><place w:st="on">ADO</place></city>, and COM objects to provide a common syntax to access their properties and methods.  

Example
$g = Get-WmiObject Win32_Process
$g[0].Name  # instead of $g[0].Properties[“Name”]

 

Arithmetic Binary Operators

+

addition, concatenation

-

Subtraction

*

multiplication, string repetition

/

Division

%

Modulus

 

Array Operations

Does this array have a <metricconverter w:st="on" productid="3 in">3 in</metricconverter> it

1,2,3,5,3,2 –contains 3


Return all elements equal to 3:

1,2,3,5,3,2 –eq 3

 

Return all elements less than 3:

1,2,3,5,3,2 –lt 3

 

Test if 2 exists in collection:

if (1, 3, 5 –contains 2) …

Other operators:  -gt, -le, -ge, -ne

 

Arrays

“a”,“b”,”c”

array of strings

1,2,3

array of integers

@()

empty array

@(2)

array of 1 element

1,(2,3),4

array within array

,”hi”

Array of one element

$a[5]

sixth element of array*

$a[2][3]

fourth element or the third

 

element of an array

$a[2..20]

Return elements 3 thru 21

·        Arrays are zero based.

 

Assignment Operators

=, +=, -=, *=, /=, %=

 

 

Associative Arrays (Hashtables)

$hash = @{ }

Create empty hashtable

$h =@{foo=1;bar=2}

Create and initialize a hashtable

$hash.key1 = 1

Assign 1 to key “key1”

$hash.key1

Returns value of key1

$hash["key1"]

Returns value of key1

Boolean Values and Operators

TRUE

FALSE

$TRUE

$FALSE

Any string of length > 0 except the word “false”

Empty string or the string “false”

Any number !=0

Any number = 0

Array of length > 1

Array of length 0

Array of length 1 whose element is TRUE

Array of length 1 whose element is FALSE

A reference to any object

Null

Break (Scripting)

The break commands exits a loop. It can take an optional LABEL to break to

Example:

while (1)

{        $a = something

          if ($a –eq 1) break;

}

 

Command Expansion Operators

$( )

Returns null

$(1,2,3)

Returns an array containing1,2,3.

$(Get-Alias a*)

Returns evaluation of the expression

@(Get-Alias;Get-Process)

Executes the two commands and returns the results in an array

Comments

# This is a comment because # is the first char of a token

$a = “#This is not a comment…”

$a = “something” # …but this is.

Write-Host Hello#world

 

Comparison Operators

-eq

Equal

-ne

Not equal

-gt –ge

Greater than, greater than or equal to

-lt –le

Less than, less than or equal to

 

“i” or “c” may be prepended to get case-insensitive or case-sensitive operations (for example, –ceq )

Continue (Scripting)

The continue statement continues the next iteration of a loop without breaking out of it. Example:

while (1)

{               $a = something

      if ($a –eq 1) (continue)

      # This line is not reached unless $a == 1

}

#  This line is never reached.

Dot Sourcing

Dot sourcing allows running functions, script blocks, and scripts in the current scope rather than a local one. Example:

. MyFunction

 

If MyFunction sets a variable, it is set in the current scope rather than the function’s local scope.

$a = {$x = Get-Process | Select –First 2}

. $a    #Evaluates the script block in the current scope

 

Escape Sequences

The Windows PowerShell escape character is the backwards apostrophe, or `.  To make a character literal, precede it with `.  To specify a ` use ``.

Special escape sequences

`0

(null)

`a

(alert)

`b

(backspace)

`f

(form feed)

`n

(new line)

`r

(carriage return)

`t

(tab)

`v

(vertical quote)

 

Execution Order

Windows PowerShell attempts to resolve commands in the following order: aliases, functions, cmdlets, scripts, executables, and normal files.

 

For (Scripting)

[:label] for ([initializer]; [condition]; [iterator]) {}

 

Example:

for ($i = 0; $i –lt 5; $i++) {Write-Object $i}

 

Foreach (Scripting)

[:label]

foreach (identifier in collection) {}

Expression | foreach {}

Expression | foreach {BEGIN{} PROCESS{} END{}}

 

Examples:

$i = 1,2,3

foreach ($z in $i) {Write-Object $z}

Get-Process |foreach {BEGIN{$x=1}

       PROCESS{$X++}

       END{“$X Processes”}}

 

Functions (Scripting)

function MyFunction {

      write-object $args[0]

}

 

function test ([string]$label=”default label”,[int]$start=0)

{ BEGIN {$x=$start} PROCESS {“$label: $_”’; $x++}

     END{“$x total”}

}

Filters (Scripting)

padding-right: 5.4pt; border-top: #ece9d8; padding-left: 5.4pt; padding-bottom: 0cm; width: 216pt; padding
分享到:
评论

相关推荐

    WINDOWS POWERSHELL 4.0 LANGUAGE QUICK REFERENCE - 4页

    Windows PowerShell 4.0是一种强大的命令行环境,用于管理和自动化Windows操作系统及相关的应用程序。它引入了许多功能和操作符,使得脚本编写和任务自动化变得更加高效。以下是对标题和描述中涉及的一些关键知识点...

    卸载以前版本的 Windows PowerShell 和 Windows 远程管理

    卸载以前版本的 Windows PowerShell 和 Windows 远程管理是指在安装 Windows Management Framework(包括 Windows PowerShell 2.0 和 WinRM 2.0)前,需要卸载 Windows PowerShell 和 Windows 远程管理 (WinRM) 的较...

    Reflector.PowerShellLanguage.zip

    标题 "Reflector.PowerShellLanguage.zip" 暗示我们正在讨论一个与PowerShell语言相关的工具或库,可能是用于代码分析、反编译或调试的。Reflector通常指的是Red Gate的.NET反编译器,它允许开发者查看和理解.NET...

    WindowsPowerShell v1.0

    Windows PowerShell是微软为Windows操作系统开发的一个强大的命令行脚本环境,它在Windows系统管理中扮演着重要的角色。PowerShell v1.0是该工具的早期版本,发布于2006年,它提供了比传统的CMD命令行更加强大、灵活...

    Windows PowerShell 2.0 for xp下载

    Microsoft Windows PowerShell 是一种新的命令行外壳和脚本语言,用于进行系统治理和自动化。Windows PowerShell 建立在 .NET Framework 的基础上,IT 专业人员可通过命令或脚本来治理计算机,从而通过系统自动化来...

    Windows PowerShell 1.0 xp x86(32位) KB926139 ENU

    Windows PowerShell 1.0 是微软推出的一个强大的命令行脚本环境,主要针对系统管理员和开发者设计,用于管理和自动化Windows操作系统。在本案例中,我们关注的是"Windows PowerShell 1.0 xp x86(32位) KB926139 ENU...

    Windows PowerShell入门指南

    ### Windows PowerShell 入门知识点详解 #### Windows PowerShell 设计理念及功能 - **设计初衷**:Windows PowerShell 被设计成一种新型的 Windows 命令行外壳程序,旨在为系统管理员提供强大的命令行和脚本环境...

    Windows PowerShell 1.0

    **Windows PowerShell 1.0** 是微软为Windows操作系统推出的一个强大的命令行脚本环境,它在管理和自动化任务方面提供了极大的便利。在安装SQL Server 2008时,Windows PowerShell 1.0作为一项先决条件,是确保SQL ...

    WindowsPowerShell实战指南 高清带书签第2版.zip

    《Windows PowerShell实战指南》第二版是一本专注于提升用户在Windows操作系统环境下使用PowerShell进行系统管理和自动化操作的权威书籍。PowerShell是微软推出的一种命令行脚本环境,它结合了命令行接口和脚本语言...

    Windows PowerShell Step by Step ( PDFDrive.com ).pdf

    ### Windows PowerShell Step by Step (第三版) #### 一、概述 本书《Windows PowerShell Step by Step》(第三版)由微软官方认证专家 Ed Wilson 撰写,是学习 Windows PowerShell 的权威指南。本书全面覆盖了 ...

    Windows PowerShell教程合集

    精心挑选和整理的关于Windows PowerShell的教程 |powershell 技术文档 --|Powershell_技术教程.doc --|PowerShell基础教程 --|WindowsPowerShelluserGuide |powershell 中文教程 --|Windows PowerShell 入门指南...

    Windows PowerShell实战指南 第2版

    Windows PowerShell实战指南 第2版,完整中文版,包含书签

    WindowsXP-KB968930-x86-CHS(Windows PowerShell 2.0).rar

    2. **脚本语言**:PowerShell支持一种名为PowerShell Scripting Language的脚本语言,它基于.NET Framework,允许用户编写强大的脚本来自动化日常管理任务。这种语言具有语法简洁、面向对象的特点,支持错误处理和...

    Windows PowerShell

    **Windows PowerShell** Windows PowerShell是微软开发的一个强大命令行接口和脚本环境,它极大地扩展了传统的命令提示符(CMD.exe)的功能。PowerShell基于.NET Framework,允许管理员和开发者使用命令行执行任务...

    Windows Powershell入门指南(中文)-微软资料.pdf

    ### Windows PowerShell 入门知识点详解 #### Windows PowerShell 设计理念与目标 Windows PowerShell 是微软针对系统管理员需求设计的一款革命性的命令行外壳程序。不同于传统的基于文本的命令行工具,PowerShell...

    Windows PowerShell 6: Essentials for Administration

    Windows PowerShell 6: Essentials for Administration (IT Pro Solutions) by William Stanek English | 6 Apr. 2017 | ASIN: B06VWVJYB2 | 302 Pages | AZW3 | 647.68 KB Covers all release versions of ...

    Windows Powershell

    Windows PowerShell是微软公司推出的一种强大的命令行和脚本语言,它自2006年首次推出以来,已经逐渐取代了老旧的Windows命令提示符(cmd.exe)和批处理文件(.bat或.cmd文件)成为系统管理员和开发者的首选工具。...

    powershell4.0完整升级包(windows powershell升级补丁系统4.0版本)

    powershell4.0完整升级包(windows powershell升级补丁系统4.0版本) windows08R2 powershell升级到4.0的完整升级包,包含所有需要的依赖包。 注意:只适用于08R2系统; 另外:powershell4.0功能强大 可以批量添加AD域...

    Windows Powershell和Windows Terminal的区别

    PowerShell(包括Windows PowerShell和PowerShell Core)是微软公司开发的任务自动化和配置管理框架。 UNIX系统一直有强大的壳程序(shell),Windows PowerShell的诞生就是要提供功能相当于UNIX系统的命令行壳程序...

    Windows PowerShell每周提示

    Windows PowerShell每周提示

Global site tag (gtag.js) - Google Analytics