[玩转系统] PowerShell 运算符 |几种 PowerShell 运算符的详细信息
作者:精品下载站 日期:2024-12-14 04:58:11 浏览:11 分类:玩电脑
PowerShell 运算符 |几种 PowerShell 运算符的详细信息
什么是 PowerShell 运算符?
我们主要使用PowerShell Operators来执行命令。如果您想做某事,您可以在网络上搜索该命令并复制该命令并将其粘贴到命令提示符或 PowerShell 中。但真正的 PowerShell 是一个面向对象的自动化工具,而不是 DOS 命令行界面。
Hadoop、数据科学、统计及其他
PowerShell 运算符
在本节中,我将解释PowerShell支持的常用运算符。但它有很多运营商。我在下面列出的
- 算术运算符(+、-、*、/、%)
- 赋值运算符(=、+=、-=、*=、/=、%=)
比较运算符
- 相等运算符(-eq、-ne、-gt、-lt、-le、-ge)
- 匹配运算符(-match、-notmatch、-replace)
- 包含比较运算符(-in、-notin、-contains、-notcontains)
- 逻辑运算符(-and、-or、-xor、-not、!)
- 重定向运算符(>、>> 和 >&1)
- 拆分和连接运算符(-split、-join)
- 类型运算符(-is、-isnot、-as)
- 一元运算符 (++, -)
- 特殊操作员
不同类型的 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 % 32. 赋值运算符
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 !05. 重定向运算符
它们用于将一个命令的输出重定向为另一命令的输入。
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’
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag