当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 运算符 |几种 PowerShell 运算符的详细信息

作者:精品下载站 日期:2024-12-14 04:58:11 浏览:11 分类:玩电脑

PowerShell 运算符 |几种 PowerShell 运算符的详细信息


[玩转系统] PowerShell 运算符 |几种 PowerShell 运算符的详细信息

什么是 PowerShell 运算符?

我们主要使用PowerShell Operators来执行命令。如果您想做某事,您可以在网络上搜索该命令并复制该命令并将其粘贴到命令提示符或 PowerShell 中。但真正的 PowerShell 是一个面向对象的自动化工具,而不是 DOS 命令行界面。

Hadoop、数据科学、统计及其他

PowerShell 运算符

在本节中,我将解释PowerShell支持的常用运算符。但它有很多运营商。我在下面列出的

  1. 算术运算符(+、-、*、/、%)
  2. 赋值运算符(=、+=、-=、*=、/=、%=)
  3. 比较运算符

    • 相等运算符(-eq、-ne、-gt、-lt、-le、-ge)
  4. 匹配运算符(-match、-notmatch、-replace)
  5. 包含比较运算符(-in、-notin、-contains、-notcontains)
  6. 逻辑运算符(-and、-or、-xor、-not、!)
  7. 重定向运算符(>、>> 和 >&1)
  8. 拆分和连接运算符(-split、-join)
  9. 类型运算符(-is、-isnot、-as)
  10. 一元运算符 (++, -)
  11. 特殊操作员

不同类型的 PowerShell 运算符

PowerShell 运算符易于使用。在这里,我们将通过示例讨论如何使用 PowerShell 运算符。

1. 算术运算符

算术运算符计算数值。您可以进行加法、减法、乘法、除法、余数运算。另外+和*对字符串、数组和哈希表进行操作。

Operator Description Example + Adds numeric values 6 + 2 concatenates a string, arrays, and hash tables “edu” + “CBA” – Subtracts numeric values 8 – 9 Makes a number negative -98 * Multiple numeric values 6 * 2 copy string, arrays to the specified number of times “!” * 3 / Divides numeric values 9 / 7 % Gives remainder after division 9 % 3

2. 赋值运算符

PowerShell 赋值运算符为变量分配、更改或附加值。

Operator Description Example = Assign value to variable $a = 3 += Adds and assign value to the variable $a += 4 Concatenates string at the end $b = “Hello, “
$b += “World” Adds number to array $a = 1,2,3
$a += 2 -= Subtracts and assign value to the variable $a -= 9 *= Multiplies and assign value to the variable $a *= 2 Appends string with a specified number of times $e = “String “
$e * 5 /= Divides and assign a value to the variable $a /= 7 %= Divides and assign a remainder value to the variable $a %= 3

3. 比较运算符

  • 相等运算符

检查值是否相等。这包括数字、字符串、数组。它将返回 True 或 False 的结果。

Operator Description Example -eq Check for equal value 1 -eq 1 Check for equal arrays 1,2,3 -eq 2 Check for equal strings “Hello” -eq “World” -ne Check for non-equal value 1 -ne 2 Check for non-equal arrays 1,2,3 -ne 2 Check for non-equal strings “Hello” -ne “World” -gt Check for greater value 8 -gt 6 Check all greater values in array and prints one by one 7, 8, 9 -gt 8 -ge Check for greater or equal value 8 -ge 8 Check all greater values or equal values in array and prints one by one 7, 8, 9 -ge 8 -lt Check for lesser value 8 -lt 6 Check all lesser values in array and prints one by one 7, 8, 9 -lt 8 -le Check for lesser or equal value 6 -le 8 Check all lesser values or equal values in array and prints one by one 7, 8, 9 -le 8
  • 匹配运算符

这些 PowerShell 运算符能够使用通配符表达式查找具有特定模式的元素。

Operator Description Example -match Matches a string with a specified regular expression “Sunday”, “Monday”, “Tuesday” -match “sun” -notmatch Does not match a string with a specified regular expression “Sunday”, “Monday”, “Tuesday” -notmatch “sun” -replace Check for the given string and replace with specified string “book” -replace “B”, “C”
  • 包含比较运算符

这些 PowerShell 运算符用于检查数组中是否存在指定元素或数组。

Operator Description Example -contains Checks for the existence of a specified element in an array “red”, “yellow” -contains “red” -notcontains Checks for the non-existence of specified element in an array “red”, “yellow” -notcontains “green” -in Checks for the existence of a specified element in an array “red” -in “red”, “yellow” -notin Checks for the non-existence of specified element in an array “green” -notin “red”, “yellow”

注意:contain和in执行相同的操作,操作数顺序不同,在“contains”中我们取右边的值来检查左边的值。但在“in”中,我们采用左侧值来检查右侧值。

4. 逻辑运算符

它还允许我们使用 AND、OR、NOT、XOR 等逻辑运算。

Operator Description Example -and Truth with both statements is TRUE. 1 -and 1 -or Truth with any one of the statements is TRUE. 1 -or 0 -xor Truth when only of the statement is TRUE. 1 -xor 0 -not Negates the statement. -not 1 ! Negates the statement !0

5. 重定向运算符

它们用于将一个命令的输出重定向为另一命令的输入。

Operator Description Example > Send all success stream data to output .\script.ps1 > script.log >> Appends all success stream data to output .\script.ps1 >> script.log n>&1 Redirects a specified stream (n) to output .\script.ps1 3>&1 script.log

注意

  • 3>&1 - 用于警告重定向
  • 2>&1 - 用于错误重定向

6. 拆分和连接运算符

该 PowerShell 运算符用于将一个命令的输出重定向为另一命令的输入。

Operator Description Example -split Splits a string into to substring based on a delimiter -split “one two three four” Splits string with a specified delimiter “Lastname:FirstName:Address” -split “:” -join Joins given strings to a single string -join “a”, “b”, “c”

7. 类型运算符

类型运算符告知给定对象是否是指定 .NET 类型的实例。

Operator Description Example -is Compares instance of the specified .NET type and return True if equal. 32 -is “int” -isNot Compares the instance of specified .NET and return False if not equal. 32 -isNot “int” -as Converts the given value to the specified type “12/31/07” -as [DateTime]

8. 一元运算符

这些是 PowerShell 中用于递增和递减的快速运算符。主要用于迭代。

Operator Description Example ++ Increments value by 1and assign back $a = 9

$a++ — Decrement value by 1 and assign back $a = 9

$a–

9. 特殊操作员

PowerShell 特殊运算符具有不适合任何其他组的特定用例。

Operator Description Example @( ) Display the result of one or more statements as a list. @(Get-WmiObject win32_logicalDisk) & Runs a command, script or block. $c = “get-executionpolicy”

& $c [ ] Converts object to specified type [int64]$a = 34 , Comma creates an array. $myArray = 1,2,3 -f Format operator, formats string by format methods “{0} {1,-10} {2:N}” -f 1,”hello”,[math]::pi .. Generates a range ‘a’..’f’

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯