[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例
作者:精品下载站 日期:2024-12-14 04:50:17 浏览:14 分类:玩电脑
PowerShell 加入路径 | PowerShell 连接路径示例
PowerShell 连接路径简介
以下文章提供了 PowerShell Join-Path 的概述。在某些情况下,用户可能希望将两条路径合并为一条路径;在这种情况下,用户应使用 join-path cmdlet。将有一个父路径,并且可以将多个子路径附加到父路径以创建新路径。这可以与多个提供程序一起使用,例如注册表、文件系统和证书等。路径参数以 PowerShell 易于识别的格式操作并返回路径。这里我们将详细了解连接多条路径的各种参数和方式。
PowerShell 连接路径的语法
Join-Path cmdlet 的语法如下:
姓名:
Join-Path
语法:
Join-Path [-Path] <string[]> [-ChildPath] <string> [-Resolve] [-Credential <pscredential>] [-UseTransaction]
[<CommonParameters>]
别名:
None
示例:
代码:
Write-Host "Demo of join path" -ForegroundColor Green
$path1="test"
$path2="test1"
Write-Host "Path one is " $path1
Write-Host "Path two is" $path2
Write-Host "Combined path is" -ForegroundColor Green
Join-Path -Path $path1 -ChildPath $path2
输出:
参数:
- -AdditionalPath: 该参数的数据类型为String[]。这表示需要添加到路径的额外值。尽管此参数是可选且指定的,但子路径参数必须提供一个值。使用此参数可以连接无限数量的路径。此参数在 cmdlet 中的位置为 2。None 是此参数的默认值。它接受管道输入,但该参数不支持通配符。
- -ChildPath:此参数是必需的。这表示将附加到路径参数值的值。它在 cmdlet 中的位置是 1。该参数的数据类型是字符串。 None 是其默认值。它接受通配符和管道输入。
- -Credential:此参数仅用于模拟目的。数据类型为 PSCredential,并且是可选的。默认值为无。这可以接受管道输入,但不允许使用通配符。
- -Path:这表示子路径将附加到的主路径值。这是一个强制参数。数据类型为 String[],别名为 PSPath。此参数在 cmdlet 中的位置为 0。默认值为 none。它接受管道输入,也允许使用通配符。
- -Resolve:这表示 cmdlet 应使用当前提供程序来解析连接路径。如果提供通配符,cmdlet 将返回与连接路径对齐的所有路径。如果不使用通配符,如果不匹配,则会抛出错误。该参数的数据类型是一个开关。这是一个可选参数。默认值为无。它不接受管道输入和通配符。
PowerShell 连接路径示例
下面给出了 PowerShell Join-Path 的示例:
例子#1
连接一条父路径和一条子路径。
代码:
Write-Host "Welcome to the demo of joining one parent and one child path" -ForegroundColor Green
$parentpath=Read-Host "Enter the parent path"
$childpath= Read-Host "Enter the child path"
Write-Host "Combined path is as follows" -ForegroundColor Green
Join-Path -Path $parentpath -ChildPath $childpath
输出:
例子#2
使用解析和通配符显示组合路径的内容。
代码:
Write-Host "Demo of using resolve parameter" -ForegroundColor Green
Write-Host "Demo of providing wild card matches in both parent and child" -ForegroundColor Green
$parent= Read-Host "Enter the parent path with wildcard"
$childp= Read-Host "Enter the child path with wildcard"
Write-Host "Display the contents using the join-path" -ForegroundColor Green
Join-Path $parent $childp -Resolve
输出:
在上面的示例中,父路径提供了一个值 c:\vignesh\t*,这意味着在 Vignesh 文件夹内,任何以 t 开头的子文件夹都被视为子路径“*”中的父值,即表示一切。因此,输出是匹配条件的所有文件的列表的显示。
例子#3
附加路径参数的使用。
代码:
Write-Host "Welcome to demo of additional child path in Join-path" -ForegroundColor Green
$mainpath= Read-Host "Enter the main path value"
$cpath= Read-Host "Enter the child path value"
$apath1= Read-Host "Enter the additional path one"
$apath2 = Read-Host "enter the additional path two"
$apath3 = Read-Host "enter the additional path three"
$apath4 = Read-Host "enter the additional path four"
$apath5 = Read-Host "enter the additional path five"
Write-Host "Joining all the path" -ForegroundColor Green
$path = Join-Path -Path $mainpath -ChildPath $cpath -AdditionalChildPaths($apath1, $apath2,$apath3,$apath4,$apath5)
Write-Host "the joined path is" -ForegroundColor Green
输出:
要查找此 cmdlet 支持的提供程序列表,我们可以运行 get-psprovider cmdlet 来识别这些提供程序。
以下是当前会话支持的提供程序:
例子#4
使用管道连接多个路径。
代码:
Write-Host "Welcome to demo of joining path using pipeline input" -ForegroundColor Green
$mainpath= Read-Host "Enter the main path value"
$cpath= Read-Host "Enter the child path value"
$apath1= Read-Host "Enter the additional path one"
$apath2 = Read-Host "enter the additional path two"
$apath3 = Read-Host "enter the additional path three"
$apath4 = Read-Host "enter the additional path four"
$apath5 = Read-Host "enter the additional path five"
Write-Host "Joining all the path using pipeline input" -ForegroundColor Green
Join-Path $mainpath | Join-Path -ChildPath $cpath | Join-Path -ChildPath $apath1 | Join-Path -ChildPath $apath2 | Join-Path -ChildPath $apath3 | Join-Path -ChildPath $apath4 | Join-Path -ChildPath $apath5
输出:
在加入时,在上面的 cmdlet 中,如果指定了子路径参数,则会抛出一条错误,指出 cmdlet 不接受管道输入。为了避免这种情况发生,从第二条路径开始,所有路径前面都必须带有子路径参数。
结论
因此,本文介绍了有关 Join-Path cmdlet 及其各种强制和可选参数的详细信息。它还展示了使用 Join-Path cmdlet 以不同方式连接路径的各种示例。
猜你还喜欢
- 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