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

[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例

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

PowerShell 加入路径 | PowerShell 连接路径示例


[玩转系统] 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

输出:

[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例

参数:

  • -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

输出:

[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例

例子#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

输出:

[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例

在上面的示例中,父路径提供了一个值 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

输出:

[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例

要查找此 cmdlet 支持的提供程序列表,我们可以运行 get-psprovider cmdlet 来识别这些提供程序。

以下是当前会话支持的提供程序:

[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例

[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例

例子#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

输出:

[玩转系统] PowerShell 加入路径 | PowerShell 连接路径示例

在加入时,在上面的 cmdlet 中,如果指定了子路径参数,则会抛出一条错误,指出 cmdlet 不接受管道输入。为了避免这种情况发生,从第二条路径开始,所有路径前面都必须带有子路径参数。

结论

因此,本文介绍了有关 Join-Path cmdlet 及其各种强制和可选参数的详细信息。它还展示了使用 Join-Path cmdlet 以不同方式连接路径的各种示例。

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

取消回复欢迎 发表评论:

关灯