[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表
作者:精品下载站 日期:2024-12-14 04:47:20 浏览:14 分类:玩电脑
PowerShell 转义字符 | PowerShell 中的转义序列列表
PowerShell 转义字符简介
在 PowerShell 中,转义字符是不属于标准字符集的特殊字符序列。它们用于指示 PowerShell 编译器如何处理序列中的下一个字符。转义字符是 PowerShell,通常以反引号 (`) 为前缀,这意味着必须以字面方式处理该字符,而不是以其他方式处理。反引号字符也称为重音符号,其 ASCII 值为 96。转义序列必须在“ ”内使用才能被视为转义序列。
语法:
以下是转义序列的示例
`t-
这用于水平制表符间距。
代码:
Write-Host "first word" `t "second word"
输出:
PowerShell 中可用转义序列的列表
以下是 PowerShell 中可用的一些转义序列。
Escape Sequence使用
`0这表示空字符
`a这是为了警报
`b这表示退格键
`e这是用于转义序列的
`f这是用于换页的
`n这表示新行
`r这是用于回车的
`t这是用于水平选项卡的
`u{x}这是 Unicode 的转义序列
`v这表示垂直制表符空间
1.`0
这用于在 PowerShell 输出中插入空格。这与 $null 不同,$null 存储空值。这用于处理具有终止字符的文本文件。
代码:
Write-Host "Example of null character escape sequene"
Write-Host "null" `0 "character"
输出:
2.`一个
这用于产生蜂鸣声。这用于警告目的或向用户发送提醒。以下脚本用于产生三声蜂鸣声。
代码:
Write-host “Alert sound example”
for ($i = 0; $i -le 2; $i++)
{
"`a"
}
输出:
3. 退格键 (`b)
该转义序列用于将光标位置向后移动一位,但不会删除字符。
代码:
Write-Host "Example of back space escape sequene"
$input= "Vignesh Krishnakumar"
Write-Host "Actual word is "$input
Write-Host "after using back space"
Write-Host "vignesh`b`b`bkrishnakumar"
输出:
4. 转义符 (`e)
这用于格式化文本目的,例如修改颜色或为文本添加下划线。这些也可用于定位光标。
5.换页(`f)
这在打印文档期间使用。这将弹出当前页面并启用连续打印。
6. 新行(`n)
这用于在前一个字符之后立即插入新行。
代码:
Write-Host "new line escape sequence example"
Write-Host "first line `nsecond line `nthird line"
输出:
7. 回车(`r)
这用于擦除插入点之前的行。
代码:
Write-Host "carriage return escape sequence example"
Write-Host "first line second line third line "
write-host "deleting everything in above example"
Write-Host "`rprevious characters are deleted"
输出:
8. 水平制表符 (`t)
这用于在输出中插入制表符空格。默认情况下,PowerShell 每八个空格引入一个制表符。
代码:
Write-Host "horizontal escape sequence example"
Write-Host "first word `t second word `t vignesh `t krishnakumar"
输出:
9. 停止解析令牌(- -%)
当需要将输入传递给其他 cmdlet 时,将使用此序列。这可以防止 PowerShell 将字符串解释为 cmdlet 或表达式。
代码:
Write-Host "PowerShell escape sequence example"
Write-Host "escaping # character"
echo one `# two
Write-Host "new line escape sequence"
Write-Host "one `ntwo `nthree"
Write-Host "horizontal tab escape sequence"
write-Host "vignesh`tkrishnakumar`tis working`tfrom chennai `the is `t a freelancer"
Write-Host "demo of semicolon to split strings"
Write-Host "first word ; second word"
Write-Host "escaping single and double quotes"
$msg1 = 'Every "man" should pay $5000 to get "free" food'
write-Host "messages is" $msg1
$msg2="Every ""man"" should bring `$5"
write-Host "message is" $msg2
输出:
代码:
Write-Host "PowerShell escape sequence example"
Write-Host "escaping # character"
echo 33#44#55
Write-Host "new line escape sequence"
Write-Host "first word `second word `third word"
Write-Host "horizontal tab escape sequence"
write-Host "this`is`tis working`as dfsd `sdfdsf is `t dsa sdf"
Write-Host "demo of semicolon to split strings"
Write-Host "dan brown ; j k rolwing"
Write-Host "escaping single and double quotes"
$msg1 = 'Every "sdfdsf" sfdsf pay $33434 to sdfdsf "sdfdf" sfdsdf'
write-Host "messages is" $msg1
$msg2="sdfdsf ""sfdsf"" sfdsf sfdf `$5234324"
write-Host "message is" $msg2
输出:
常见错误如何处理?
代码:
write-host "Demo of single and double quotes"
write-output -inputobject 'My favorite `tteacher is 'raji''
Write-Host "The above will throw an error"
Write-Host "Correct usage is as below"
write-output -inputobject "My favorite `tteacher is 'raji'"
输出:
抛出错误是因为单引号包含在单引号内。通过在双引号内使用单引号可以避免这种情况。
代码:
Write-Host "Error with space example"
write-output -inputobject vignesh krishnakumar
Write-Host "Error is thrown"
Write-Host "Correct usage is as follows"
Write-Output -inputobject vignesh` Krishnakumar
输出:
发生错误是因为未向 PowerShell 正确指示空间。
使用正则表达式转义特殊字符
代码:
Write-Host "Escaping special characters demo"
Write-Host "Escaping backslash in path"
[regex]::Escape('C:\Vignesh\New folder')
Write-Host "Escape a string"
[regex]::Escape('Vignesh!!!Krishnakumar!!!')
[regex]::Escape('What is your age?')
输出:
结论
因此,本文详细介绍了 PowerShell 中的转义字符。它还详细介绍了各种可用的 PowerShell 转义序列,并通过详细示例对每个序列进行了说明。还详细解释了单引号和双引号使用过程中可能出现的各种错误以及如何处理。本文还深入介绍了如何转义特殊字符,并通过适当的示例进行了演示。它展示了如何使用正则表达式来避免特殊字符的示例。要详细了解,建议编写示例脚本并使用它们解决方法。
猜你还喜欢
- 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