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

[玩转系统] 什么是PWSH.EXE?您可以使用的 PWSH 语法列表

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

什么是PWSH.EXE?您可以使用的 PWSH 语法列表


PowerShell 是 Microsoft 创建的最强大的脚本工具之一。在这篇文章中,我们将分享什么是 PWSH.exe,以及重要的 PWSH 语法列表。我确信许多用户都使用过 Windows PowerShell,但 PWSH 现在是一个跨平台脚本工具,可以在 Windows、macOS 和 Linux 上运行。但是,WSL(即 Linux 上的 Windows 子系统)不支持它,尝试将 PWSH 设置为登录 shell 将导致 WSL 不稳定。

什么是PWSH.EXE?

[玩转系统] 什么是PWSH.EXE?您可以使用的 PWSH 语法列表

在开始之前,让我们先弄清楚一个细节。 PWSH.EXE 是 PowerShell 的新名称。从版本 6 开始,它被称为 PowerShell Core。它之前被命名为 powershell.exe,您一定已经在 Windows(版本 5.1)中看到过它的安装。难怪每次在 Windows 中启动 PowerShell 时,您都会收到一条消息:

“尝试新的跨平台 PowerShell https://aka.ms/pscore6。 ”

您可以了解 PowerShell 和 PowerShell Core 之间的区别。

快进到今天,PowerShell 已经到达版本 7,与版本 6 相比,这是一个重大变化,并使用 .NET Core 3 而不是 .Net Framework。如果您想尝试一下,请了解如何在 Windows 10 上安装 PowerShell 7.0。

重要的 PWSH 语法

-文件 | -f:如果您将命令写入脚本文件,则可以将其用作输入。如果适用,您还可以为文件提供参数。

pwsh -File .\test.ps1 -TestParam $env:windir

-命令| -c:使用它来执行命令或ScriptBlock。 ScriptBlock 是包含在 {} 内的函数集

pwsh -Command {Get-WinEvent -LogName security}

或者

@'
"in"

"hi" |
% { "$_ there" }

"out"
'@ | powershell -NoProfile -Command -

-EncodedCommand | -e | -ec:当需要使用复杂的引号或大括号时使用此选项。

$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
pwsh -encodedcommand $encodedCommand

-登录 | -l:在 Linux 和 macOS 上,将 PowerShell 作为登录 shell 启动,使用 /bin/sh 执行登录配置文件,例如 /etc/profile 和 ~/.profile。它不适用于Windows。

您必须验证 /etc/shells 下列出的绝对路径。您可以使用chsh实用程序将当前用户的 shell 设置为 pwsh。

chsh -s /usr/bin/pwsh

-设置文件| -设置

如果您想使用本地项目设置覆盖全局设置,则可以使用此选项指定设置文件。系统范围的设置可在 powershell.config.json 中找到。

pwsh -SettingsFile c:\myproject\powershell.config.json

PWSH 语法的完整列表

pwsh[.exe]
[[-File] <filePath> [args]]
[-Command { - | <script-block> [-args <arg-array>]
| <string> [<CommandParameters>] } ]
[-ConfigurationName <string>]
[-CustomPipeName <string>]
[-EncodedCommand <Base64EncodedCommand>]
[-ExecutionPolicy <ExecutionPolicy>]
[-InputFormat {Text | XML}]
[-Interactive]
[-Login]
[-MTA]
[-NoExit]
[-NoLogo]
[-NonInteractive]
[-NoProfile]
[-OutputFormat {Text | XML}]
[-SettingsFile <SettingsFilePath>]
[-STA]
[-Version]
[-WindowStyle <style>]
[-WorkingDirectory <directoryPath>]

pwsh[.exe] -h | -Help | -? | /?

如果您需要更多详细信息,可以访问 docs.microsoft.com。

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

取消回复欢迎 发表评论:

关灯