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

[玩转系统] Powershell 在命令中使用变量

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

Powershell 在命令中使用变量


PowerShell 变量存储值以便在命令中灵活使用。命令中变量的替换、串联和内联使用有助于动态命令创建。

在本文中,我们将讨论如何以多种方式在命令中使用变量。

在命令中使用变量

要在 cmdlet 中使用变量,请在 Get-Content cmdlet 字符串中使用它:

$filePath = Get-ChildItem -Path 'D:\PS\ad.txt' 
Get-Content $filePath   

在上面的 PowerShell 脚本中,变量 $filePath 存储使用 Get-ChildItem cmdlet 检索的文件位置。我们通过在命令字符串中使用命令 Get-Content 来直接引用变量 $filePath

上述内联变量使用的 PowerShell 脚本的输出是:

PS C:\> $filePath = Get-ChildItem -Path 'D:\PS\ad.txt'                                                                  PS C:\> Get-Content $filePath           
This is sample file.

PS C:\>                                                                                                                               

使用变量替换

使用变量替换技术在 Write-Host cmdlet 中使用变量:

$userName = "Shell Admin"

Write-Host "Hello, $userName"

在上面的 PowerShell 脚本中,变量 $username 用作 Write-Host cmdlet 字符串中的变量替换。

上述脚本的输出将消息打印为:

Hello, Shell Admin

PowerShell 在命令路径中使用变量

您可以将变量与字符串或变量连接起来,以便在 Get-Content cmdlet 中使用它:

$folderPath = 'D:\PS'
$fileName = "ad.txt"

$filePath = $folderPath + "\" + $fileName

Get-Content $filePath

在上面的 PowerShell 脚本中,$folderPath$fileName 变量分别存储目录位置和文件名。我们将它们连接到 $filePath 中,并使用 Get-Content cmdlet 中的变量来读取文件的内容。

上述脚本的输出读取文件的内容。

在 CMD 中使用 PowerShell 变量

您可以使用 cmd.exe 在 cmd 命令中使用 PowerShell 变量:

$folderPath = "D:\PS"                                                                                           cmd.exe /c "dir $folderPath"  

在上面的 PowerShell 脚本中,变量 $folderpath 包含目录位置。它在 cmd 命令中使用,使用 cmd.exe 获取指定路径中可用的文件和文件夹。

上述脚本的输出是:

PS C:\> $folderPath = "D:\PS"                                                                                           
PS C:\> cmd.exe /c "dir $folderPath"                                                                                     

Volume in drive D is Local Disk
Volume Serial Number is CA4B-D81A

 Directory of D:\PS

07-04-2023  12:10    <DIR>          .
07-04-2023  12:10    <DIR>          ..
16-07-2022  22:08    <DIR>          AD
07-04-2023  12:10                 0 ad.txt
26-02-2023  16:10                87 comp.txt
15-08-2022  20:06    <DIR>          Config

PowerShell 在命令行中使用变量

您可以在 cmdlet 中使用该变量:

$processName = "notepad"
Start-Process $processName

在上面的 PowerShell 脚本中,$processName 变量保存进程的名称,并在命令行 Start-Process 中使用它来启动进程。

上述脚本的输出将打开记事本。

在命令中使用环境变量

PowerShell 环境变量提供对系统设置和用户配置文件的访问。您可以使用 Write-Host cmdlet 中的环境变量来访问变量值并使用它。

Write-Host "Hello, $env:USERNAME!"

在上面的 PowerShell 脚本中,$env:USERNAME 是一个用于获取当前用户名的环境变量,它在命令中用于将用户名打印到控制台。

在调用命令中使用变量

Invoke-Command cmdlet 用于在远程计算机上运行脚本块。您可以在 invoke-command cmdlet 中使用变量:

$serverName = "EU-IT-101"
$scriptBlock = {Get-ChildItem C:\Temp}
Invoke-Command -ComputerName $serverName -ScriptBlock $scriptBlock

在上面的 PowerShell 脚本中,$serverName 变量保存远程计算机名称。 invoke-command 使用-ComputerName 参数在变量名称$serverName 指定的远程计算机上运行脚本块。

上述脚本的输出运行时,将在远程计算机上运行“Get-ChildItem”cmdlet,以从远程计算机上的指定路径检索项目,并将结果返回到本地计算机。

结论

希望以上关于如何在命令中使用PowerShell变量的文章对您有所帮助。

使用 PowerShell 命令中的变量可以创建动态脚本。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯