1
0
wiki/Technology/GettingStarted/命令行工具/PowerShell/PowerShell-入门.html

668 lines
516 KiB
HTML
Raw Normal View History

2024-09-06 17:53:58 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>PowerShell 入门</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="PowerShell 入门"/><meta property="og:description" content="PowerShell 入门."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="PowerShell 入门."/><meta name="generator" content="Quartz"/><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/GettingStarted/命令行工具/PowerShell/PowerShell-入门"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"
<h3 id="概述">概述<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#概述" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 是由微软开发的一种跨平台任务自动化和配置管理框架,由命令行 Shell 和脚本语言组成。它基于 .NET 框架,可以运行在 Windows、MacOS 和 Linux 系统上。PowerShell 的强大之处在于其能够访问和管理系统的所有资源,包括文件系统、注册表、网络等。它不仅适用于系统管理员进行日常管理任务的自动化,还适用于开发者进行复杂的脚本编写和配置管理。通过 PowerShell用户可以轻松地执行复杂的操作任务自动化日常管理工作简化运维流程并提高工作效率。</p>
<p>**PowerShell 通过 cmdlet发音为 “command-let”来执行任务。**每个 cmdlet 通常由一个动词和一个名词组成,例如 <code>Get-Process</code> 用于获取进程信息。PowerShell 还支持面向对象的管道Pipeline允许 cmdlet 之间传递对象,而不仅仅是文本,这使得数据处理更加直观和强大。</p>
<h3 id="发展历史">发展历史<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#发展历史" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="初期发展">初期发展<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#初期发展" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>2006 年Windows PowerShell 1.0</strong> PowerShell 最初在 2006 年作为 Windows PowerShell 1.0 发布。这一版本基于 .NET Framework旨在为系统管理员提供一种强大的命令行工具用于自动化日常管理任务。1.0 版本的发布标志着微软在命令行和脚本语言方面迈出了重要的一步。</p>
<h4 id="版本演进">版本演进<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#版本演进" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>2008 年Windows PowerShell 2.0</strong> 2008 年,微软发布了 Windows PowerShell 2.0。此版本引入了许多新功能,包括 PowerShell ISE集成脚本环境、高级函数、远程处理Remoting和模块Modules等。这些功能显著增强了 PowerShell 的灵活性和扩展性,使其成为一个更加全面的管理工具。</p>
<p><strong>2012 年Windows PowerShell 3.0</strong> Windows PowerShell 3.0 在 2012 年随 Windows Server 2012 和 Windows 8 一同发布。此版本进一步增强了脚本语言的功能引入了工作流Workflows和改进的 cmdlet增强了对任务自动化的支持。</p>
<p><strong>2013 年Windows PowerShell 4.0</strong> 2013 年发布的 Windows PowerShell 4.0 主要带来了 DSCDesired State Configuration功能使管理员能够声明性地配置和管理系统确保系统处于期望的状态。</p>
<p><strong>2016 年Windows PowerShell 5.0</strong> Windows PowerShell 5.0 于 2016 年发布,进一步引入了许多新功能,如类和枚举、自定义模块仓库、改进的 PowerShell ISE 以及增强的 DSC 功能。</p>
<h4 id="开源与跨平台">开源与跨平台<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#开源与跨平台" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>2016 年PowerShell Core 6.0</strong> 2016 年,微软宣布 PowerShell 开源,并发布了 PowerShell Core 6.0。这是一个基于 .NET Core 的跨平台版本,能够在 Windows、MacOS 和 Linux 上运行。PowerShell Core 的发布标志着 PowerShell 从 Windows 独有工具转变为一个真正的跨平台管理框架。</p>
<h4 id="统一版本">统一版本<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#统一版本" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>2020 年PowerShell 7</strong> 2020 年,微软发布了 PowerShell 7。这一版本基于 .NET Core 3.1,进一步改进了跨平台兼容性,并统一了 Windows PowerShell 和 PowerShell Core 的功能和体验。PowerShell 7 的发布标志着 PowerShell 进入了一个新的时代,提供了更高效、更灵活的自动化和管理解决方案。</p>
<h4 id="未来发展">未来发展<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#未来发展" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>持续更新与社区参与</strong> PowerShell 的发展一直得到社区的积极参与和贡献。微软通过 GitHub 开放源码,接受社区的反馈和贡献,使 PowerShell 不断演进和改进。PowerShell 7 之后的版本将继续增强性能、扩展功能,并进一步提升跨平台支持。</p>
<h3 id="powershell-与其他-shell-的对比">PowerShell 与其他 Shell 的对比<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#powershell-与其他-shell-的对比" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>比较维度</th><th>PowerShell</th><th>CMD</th><th>Bash</th><th>Zsh</th></tr></thead><tbody><tr><td><strong>平台支持</strong></td><td>跨平台Windows, Mac, Linux</td><td>Windows</td><td>Unix 系统Linux, Mac</td><td>Unix 系统Linux, Mac</td></tr><tr><td><strong>开发公司</strong></td><td>微软</td><td>微软</td><td>GNU</td><td>开源社区</td></tr><tr><td><strong>对象模型</strong></td><td>面向对象,操作 .NET 对象</td><td>基于字符串处理</td><td>基于字符串处理</td><td>基于字符串处理</td></tr><tr><td><strong>管道机制</strong></td><td>支持对象管道</td><td>不支持</td><td>支持字符串管道</td><td>支持字符串管道</td></tr><tr><td><strong>脚本扩展名</strong></td><td>.ps1</td><td>.bat, .cmd</td><td>.sh</td><td>.zsh</td></tr><tr><td><strong>主要用途</strong></td><td>系统管理、自动化脚本</td><td>简单任务自动化、批处理</td><td>系统管理、自动化脚本、服务器管理</td><td>系统管理、自动化脚本、服务器管理</td></tr><tr><td><strong>交互界面</strong></td><td>图形界面PowerShell ISE, VS Code</td><td>无图形界面</td><td>无图形界面</td><td>无图形界面</td></tr><tr><td><strong>错误处理</strong></td><td>高级错误处理机制try/catch</td><td>简单的错误处理if errorlevel</td><td>基本错误处理</td><td>基本错误处理</td></tr><tr><td><strong>模块系统</strong></td><td>丰富的模块和包管理系统PSGallery</td><td>无模块系统</td><td>基于文件和库</td><td>基于文件和库</td></tr><tr><td><strong>脚本调试</strong></td><td>强大的调试工具(断点、变量监视)</td><td>无直接调试工具</td><td>基本调试工具set -x, trap</td><td>基本调试工具set -x, trap</td></tr><tr><td><strong>命令别名</strong></td><td>支持丰富的命令别名</td><td>支持少量命令别名</td><td>支持命令别名</td><td>支持强大的命令别名(包括 Bash 支持的所有别名)</td></tr><tr><td><strong>扩展性</strong></td><td>高度扩展性,通过 .NET 库和第三方模块</td><td>低扩展性</td><td>中等扩展性,通过脚本和第三方工具</td><td>高度扩展性,通过插件系统</td></tr><tr><td><strong>内置命令</strong></td><td>丰富的内置 cmdlet 和函数</td><td>基本的内置命令</td><td>丰富的内置命令</td><td>丰富的内置命令</td></tr><tr><td><strong>学习曲线</strong></td><td>相对陡峭,需要了解 .NET 对象模型</td><td>平缓,易于上手</td><td>中等,需要了解 Unix 系统和命令</td><td>中等,需要了解 Unix 系统和命令</td></tr><tr><td><strong>文档支持</strong></td><td>强大的内置帮助系统Get-Help</td><td>基本的帮助命令help, /?</td><td>丰富的在线文档和社区支持</td><td>丰富的在线文档和社区支持</td></tr><tr><td><strong>社区支持</strong></td><td>活跃的官方和社区支持</td><td>基本的官方和社区支持</td><td>广泛的社区支持</td><td>广泛的社区支持</td></tr></tbody></table></div>
<h2 id="安装与配置">安装与配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#安装与配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<ol>
<li>**下载:**从 <a href="https://github.com/PowerShell/PowerShell" class="external">PowerShell GitHub 页面<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a> 下载最新的 PowerShell 安装包。</li>
<li>**安装:**使用包管理器或者双击安装包,并按照提示进行安装。</li>
<li>**运行:**安装完成后Windows 可以通过 Start 菜单启动 PowerShell类 Unix 系统可以通过终端输入 <code>pwsh</code> 启动 PowerShell。</li>
<li>**更新:**可以通过 <code>Update-Help</code> 命令更新帮助文件,以便获取最新的帮助文档。</li>
<li><strong>设置执行策略(可选)</strong>PowerShell 的执行策略决定了脚本能否在系统上运行。可以使用 <code>Set-ExecutionPolicy RemoteSigned</code> 允许本地脚本和从受信任来源下载的脚本运行。</li>
</ol>
<h2 id="基础命令与语法">基础命令与语法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基础命令与语法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="命令基础">命令基础<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#命令基础" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 中的命令被称为 cmdlet发音为 “command-let”。每个 cmdlet 通常由一个动词和一个名词组成,例如 <code>Get-Process</code><code>Set-Location</code>。cmdlet 具有一致的命名规范和结构,使得命令的学习和使用更加直观和简单。可以通过以下命令获取系统中的所有 cmdlet 列表:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Command</span></span></code></pre></figure>
<p>此外,<code>Get-Command</code> 还可以用于查找特定的命令或 cmdlet。例如要查找所有与 “process” 相关的命令,可以使用:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Command</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *process*</span></span></code></pre></figure>
<h3 id="cmdlet-结构动词---名词">cmdlet 结构(动词 - 名词)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cmdlet-结构动词---名词" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>**PowerShell 的 cmdlet 以 “动词-名词” 形式命名,动词描述动作,名词描述操作对象。**例如:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Process</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 获取当前运行的进程</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Set-Location</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 设置当前工作目录</span></span></code></pre></figure>
<h4 id="常见动词">常见动词<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#常见动词" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>动词</th><th>用途说明</th><th>示例</th></tr></thead><tbody><tr><td><code>Get</code></td><td>获取信息或对象</td><td><code>Get-Process</code> 获取当前运行的进程</td></tr><tr><td><code>Set</code></td><td>设置或配置对象</td><td><code>Set-Location</code> 设置当前工作目录</td></tr><tr><td><code>New</code></td><td>创建新的对象</td><td><code>New-Item</code> 创建新文件或目录</td></tr><tr><td><code>Remove</code></td><td>删除对象</td><td><code>Remove-Item</code> 删除文件或目录</td></tr><tr><td><code>Start</code></td><td>启动操作或进程</td><td><code>Start-Process</code> 启动一个进程</td></tr><tr><td><code>Stop</code></td><td>停止操作或进程</td><td><code>Stop-Process</code> 停止一个进程</td></tr><tr><td><code>Restart</code></td><td>重启操作或进程</td><td><code>Restart-Service</code> 重启服务</td></tr><tr><td><code>Test</code></td><td>测试或验证操作</td><td><code>Test-Connection</code> 测试网络连接</td></tr><tr><td><code>Invoke</code></td><td>执行操作</td><td><code>Invoke-Command</code> 执行命令</td></tr><tr><td><code>Export</code></td><td>导出数据或对象</td><td><code>Export-Csv</code> 导出到 CSV 文件</td></tr><tr><td><code>Import</code></td><td>导入数据或对象</td><td><code>Import-Csv</code> 从 CSV 文件导入数据</td></tr><tr><td><code>Out</code></td><td>输出数据到目标</td><td><code>Out-File</code> 输出到文件</td></tr><tr><td><code>Write</code></td><td>写入数据到目标</td><td><code>Write-Output</code> 写入输出流</td></tr><tr><td><code>Read</code></td><td>读取数据</td><td><code>Read-Host</code> 从用户输入读取数据</td></tr><tr><td><code>Enable</code></td><td>启用对象</td><td><code>Enable-PSRemoting</code> 启用远程处理</td></tr><tr><td><code>Disable</code></td><td>禁用对象</td><td><code>Disable-PSRemoting</code> 禁用远程处理</td></tr><tr><td><code>Clear</code></td><td>清除数据或对象</td><td><code>Clear-Content</code> 清除文件内容</td></tr><tr><td><code>Select</code></td><td>选择数据或对象</td><td><code>Select-Object</code> 选择对象的属性</td></tr><tr><td><code>Measure</code></td><td>计算或测量数据</td><td><code>Measure-Object</code> 计算对象的属性</td></tr><tr><td><code>Compare</code></td><td>比较数据或对象</td><td><code>Compare-Object</code> 比较对象</td></tr><tr><td><code>Convert</code></td><td>转换数据格式</td><td><code>ConvertTo-Json</code> 转换为 JSON 格式</td></tr><tr><td><code>Sort</code></td><td>排序数据</td><td><code>Sort-Object</code> 对对象进行排序</td></tr><tr><td><code>Group</code></td><td>分组数据</td><td><code>Group-Object</code> 对对象进行分组</td></tr><tr><td><code>Join</code></td><td>连接数据</td><td><code>Join-Path</code> 连接路径</td></tr><tr><td><code>Split</code></td><td>分割数据</td><td><code>Split-Path</code> 分割路径</td></tr></tbody></table></div>
<h3 id="管道pipeline和对象流">管道Pipeline和对象流<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管道pipeline和对象流" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 的一个重要特性是管道Pipeline允许将一个 cmdlet 的输出直接传递给下一个 cmdlet 作为输入。管道在 PowerShell 中不仅传递文本,还可以传递对象,这使得数据处理更加直观和灵活。例如:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 上获取所有进程,并筛选出 CPU 使用率大于 100 的进程。</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Process</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Where-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">$_</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.CPU </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-gt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 100</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span></code></pre></figure>
<p>在这个示例中,<code>Get-Process</code> cmdlet 的输出传递给 <code>Where-Object</code> cmdlet 进行过滤。<code>$_</code> 是管道中的当前对象,<code>$_</code> 后面可以跟对象的属性进行操作。</p>
<p>管道的另一个常见用法是将多个 cmdlet 串联起来以完成复杂任务。例如:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取所有运行的服务,筛选出已停止的服务,并重新启动它们</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Service</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Where-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">$_</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.Status </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-eq</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'Stopped'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Start-Service</span></span></code></pre></figure>
<h3 id="获取帮助和使用-get-help">获取帮助和使用 Get-Help<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#获取帮助和使用-get-help" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>PowerShell 提供了强大的内置帮助系统,可以通过 <code>Get-Help</code> 命令获取详细的帮助信息。<code>Get-Help</code> 是学习和查找 cmdlet 详细信息的最佳方式。例如,要了解 <code>Get-Process</code> cmdlet可以使用</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Help</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-Process</span></span></code></pre></figure>
<p>使用 <code>Update-Help</code> 命令可以更新本地的帮助文件,以确保获取到最新的帮助信息:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Update-Help</span></span></code></pre></figure>
<h2 id="核心概念">核心概念<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#核心概念" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="变量">变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>在 PowerShell 中,变量以 <code>$</code> 符号开头,并且是动态类型的。这意味着你可以在变量中存储任何类型的数据,而无需提前声明变量的类型。定义和使用变量的示例如下:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义变量</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;John&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$age </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 30</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$fruits </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> @</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Apple&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Banana&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Cherry&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用变量</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Name: </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">, Age: </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$age</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Fruits: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">(</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$fruits</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -join</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ', '</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span></code></pre></figure>
<h3 id="数据类型">数据类型<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#数据类型" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>数据类型</th><th>描述</th><th>示例</th></tr></thead><tbody><tr><td>字符串String</td><td>文本数据</td><td><code>$string = &quot;Hello, World!&quot;</code></td></tr><tr><td>整数Integer</td><td>整数型数值</td><td><code>$integer = 42</code></td></tr><tr><td>浮点数Float</td><td>浮点型数值</td><td><code>$float = 3.14</code></td></tr><tr><td>数组Array</td><td>有序集合</td><td><code>$array = @(1, 2, 3, 4, 5)</code></td></tr><tr><td>哈希表Hashtable</td><td>键值对集合</td><td><code>$hashtable = @{ Name = &quot;Alice&quot;; Age = 25 }</code></td></tr></tbody></table></div>
<h3 id="算术运算符">算术运算符<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#算术运算符" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>运算符</th><th>描述</th><th>示例</th></tr></thead><tbody><tr><td><code>+</code></td><td>加法</td><td><code>$a + $b</code></td></tr><tr><td><code>-</code></td><td>减法</td><td><code>$a - $b</code></td></tr><tr><td><code>*</code></td><td>乘法</td><td><code>$a * $b</code></td></tr><tr><td><code>/</code></td><td>除法</td><td><code>$a / $b</code></td></tr><tr><td><code>%</code></td><td>取模(余数)</td><td><code>$a % $b</code></td></tr></tbody></table></div>
<h3 id="比较运算符">比较运算符<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#比较运算符" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>运算符</th><th>描述</th><th>示例</th></tr></thead><tbody><tr><td><code>-eq</code></td><td>等于</td><td><code>$a -eq $b</code></td></tr><tr><td><code>-ne</code></td><td>不等于</td><td><code>$a -ne $b</code></td></tr><tr><td><code>-gt</code></td><td>大于</td><td><code>$a -gt $b</code></td></tr><tr><td><code>-ge</code></td><td>大于或等于</td><td><code>$a -ge $b</code></td></tr><tr><td><code>-lt</code></td><td>小于</td><td><code>$a -lt $b</code></td></tr><tr><td><code>-le</code></td><td>小于或等于</td><td><code>$a -le $b</code></td></tr></tbody></table></div>
<h3 id="逻辑运算符">逻辑运算符<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#逻辑运算符" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>运算符</th><th>描述</th><th>示例</th></tr></thead><tbody><tr><td><code>-and</code></td><td></td><td><code>$a -and $b</code></td></tr><tr><td><code>-or</code></td><td></td><td><code>$a -or $b</code></td></tr><tr><td><code>-not</code></td><td></td><td><code>-not $a</code></td></tr></tbody></table></div>
<h3 id="特殊运算符">特殊运算符<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#特殊运算符" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>运算符</th><th>描述</th><th>示例</th></tr></thead><tbody><tr><td><code>-match</code></td><td>正则表达式匹配</td><td><code>$string -match &quot;Power&quot;</code></td></tr><tr><td><code>-like</code></td><td>通配符匹配</td><td><code>$string -like &quot;Power*&quot;</code></td></tr><tr><td><code>-contains</code></td><td>检查数组是否包含元素</td><td><code>$array -contains 3</code></td></tr></tbody></table></div>
<h2 id="流程控制">流程控制<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#流程控制" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="条件语句">条件语句<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#条件语句" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="ifelse-语句">if/else 语句<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#ifelse-语句" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>if</code> 语句用于根据条件执行不同的代码块。<code>else</code><code>elseif</code> 子句可以用于添加更多条件:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 10</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-gt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;a is greater than 5&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">} </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">elseif</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-eq</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;a is equal to 5&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">} </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;a is less than 5&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<h4 id="switch-语句">switch 语句<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#switch-语句" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>switch</code> 语句用于根据多个条件执行不同的代码块,可以替代多个 <code>if/else</code> 语句:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$day </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Tuesday&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">switch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($day) {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Monday&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Today is Monday&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Tuesday&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Today is Tuesday&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Wednesday&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Today is Wednesday&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> default</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Today is another day&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<h3 id="循环语句">循环语句<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#循环语句" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="for-循环">for 循环<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#for-循环" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>for</code> 循环用于执行一组语句一定次数,通常用于需要明确循环次数的情况:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; $i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-lt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; $i</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">++</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Iteration </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$i</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<h4 id="foreach-循环">foreach 循环<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#foreach-循环" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>foreach</code> 循环用于遍历数组或集合中的每个元素:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$array </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> @</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 4</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">foreach</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($item </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $array) {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Item: </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$item</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<h4 id="while-循环">while 循环<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#while-循环" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>while</code> 循环在指定条件为真时重复执行一组语句。适用于条件未知,需要动态检查的情况:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">while</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-lt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Iteration </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$i</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $i</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">++</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<h4 id="do-while-循环">do-while 循环<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#do-while-循环" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>do-while</code> 循环与 <code>while</code> 循环类似,但它至少会执行一次循环体,然后在循环体末尾检查条件:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">do</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Iteration </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$i</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $i</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">++</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">} </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">while</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-lt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
<h2 id="脚本编写">脚本编写<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#脚本编写" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>PowerShell 脚本文件的扩展名为 <code>.ps1</code>。可以使用任何文本编辑器编写脚本,然后在 PowerShell 控制台中运行:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 示例脚本hello.ps1</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;World&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Hello, </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">!&quot;</span></span></code></pre></figure>
<p>在 PowerShell 控制台中执行脚本:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.\hello.ps1</span></span></code></pre></figure>
<h3 id="脚本参数和参数化脚本">脚本参数和参数化脚本<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#脚本参数和参数化脚本" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>可以通过 <code>param</code> 关键字定义脚本参数,使脚本更灵活和可重用:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 示例脚本greet.ps1</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">param</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">string</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]$name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;World&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Hello, </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">!&quot;</span></span></code></pre></figure>
<p>在 PowerShell 控制台中传递参数执行脚本:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.\greet.ps1 </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Alice&quot;</span></span></code></pre></figure>
<h3 id="注释和文档">注释和文档<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#注释和文档" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>使用 <code>#</code> 符号添加单行注释,使用 <code>&lt;# ... #></code> 添加多行注释:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 这是单行注释</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;This is a single-line comment.&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&lt;#</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">这是多行注释</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">可以跨越多行</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#></span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;This is a multi-line comment.&quot;</span></span></code></pre></figure>
<p>良好的注释习惯可以提高脚本的可读性和维护性。例如:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义变量</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$name </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;PowerShell&quot;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 变量存储名称</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 输出问候信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Hello, </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">!&quot;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 打印问候信息</span></span></code></pre></figure>
<h3 id="示例代码">示例代码<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#示例代码" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>以下是一个示例代码,展示了如何使用常见的数据类型和运算符,并结合条件语句和循环语句,获取并输出计算机的相关信息。代码包含详细的注释以帮助理解:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="powershell" data-theme="github-light github-dark"><code data-language="powershell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义变量</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$computerName </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;LocalHost&quot;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 计算机名称</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$memoryThreshold </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 4</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 内存阈值GB</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取计算机的基本信息</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$os </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-WmiObject</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Class Win32_OperatingSystem</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$cpu </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-WmiObject</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Class Win32_Processor</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$memory </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-WmiObject</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Class Win32_PhysicalMemory</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 输出计算机基本信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Computer Name: </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$computerName</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Operating System: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">(</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$os.Caption</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;OS Version: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">(</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$os.Version</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;CPU: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">(</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$cpu.Name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Total Physical Memory: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">([</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">math</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">]::round(</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$os.TotalVisibleMemorySize</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> /</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">MB,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">))</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GB&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 检查物理内存是否低于阈值</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$totalMemoryGB </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">math</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]::round($os.TotalVisibleMemorySize </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">MB,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($totalMemoryGB </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-lt</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $memoryThreshold) {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Warning: Physical memory is below the threshold of </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$memoryThreshold</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GB.&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">} </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Physical memory is above the threshold.&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用 foreach 循环遍历和输出每条物理内存的信息</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Detailed Physical Memory Info:&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">foreach</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($mem </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $memory) {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $memCapacityGB </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">math</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]::round($mem.Capacity </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">GB,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Memory Module: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">(</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$mem.DeviceLocator</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">, Capacity: </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$memCapacityGB</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GB&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用 for 循环计算并输出每个逻辑磁盘的总大小和可用空间</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$logicalDisks </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Get-WmiObject</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Class Win32_LogicalDisk </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Filter </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;DriveType=3&quot;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 仅获取本地磁盘</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; $i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-lt</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $logicalDisks.Count; $i</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">++</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $disk </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $logicalDisks[$i]</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $totalSizeGB </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">math</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]::round($disk.Size </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">GB,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $freeSpaceGB </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">math</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]::round($disk.FreeSpace </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">GB,</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Drive: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">(</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$disk.DeviceID</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">, Total Size: </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$totalSizeGB</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GB, Free Space: </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$freeSpaceGB</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GB&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用 while 循环等待特定进程启动示例notepad.exe</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$processName </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;notepad.exe&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Waiting for </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$processName</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> to start...&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">while</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-not</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Get-Process</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;notepad&quot;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ErrorAction SilentlyContinue)) {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> Start-Sleep</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Seconds </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$processName</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> has started.&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用 switch 语句输出当前操作系统类型</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">switch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($os.Caption) {</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Microsoft Windows 10 Pro&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;You are running Windows 10 Pro.&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Microsoft Windows Server 2019&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;You are running Windows Server 2019.&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> default</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Operating system not recognized.&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Write-Output</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Script execution completed.&quot;</span></span>
<span data-line> </span></code></pre></figure></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#powershell" data-for="powershell">PowerShell</a></li><li class="depth-1"><a href="#概述" data-for="概述">概述</a></li><li class="depth-1"><a href="#发展历史" data-for="发展历史">发展历史</a></li><li class="depth-1"><a href="#powershell-与其他-shell-的对比" data-for="powershell-与其他-shell-的对比">PowerShell 与其他 Shell 的对比</a></li><li class="depth-0"><a href="#安装与配置" data-for="安装与配置">安装与配置</a></li><li class="depth-0"><a href="#基础命令与语法" data-for="基础命令与语法">基础命令与语法</a></li><li class="depth-1"><a href="#命令基础" data-for="命令基础">命令基础</a></li><li class="depth-1"><a href="#cmdlet-结构动词---名词" data-for="cmdlet-结构动词---名词">cmdlet 结构(动词 - 名词)</a></li><li class="depth-1"><a href="#管道pipeline和对象流" data-for="管道pipeline和对象流">管道Pipeline和对象流</a></li><li class="depth-1"><a href="#获取帮助和使用-get-help" data-for="获取帮助和使用-get-help">获取帮助和使用 Get-Help</a></li><li class="depth-0"><a href="#核心概念" data-for="核心概念">核心概念</a></li><li class="depth-1"><a href="#变量" data-for="变量">变量</a></li><li class="depth-1"><a href="#数据类型" data-for="数据类型">数据类型</a></li><li class="depth-1"><a href="#算术运算符" data-for="算术运算符">算术运算符</a></li><li class="depth-1"><a href="#比较运算符" data-for="比较运算符">比较运算符</a></li><li class="depth-1"><a href="#逻辑运算符" data-for="逻辑运算符">逻辑运算符</a></li><li class="depth-1"><a href="#特殊运算符" data-for="特殊运算符">特殊运算符</a></li><li class="depth-0"><a href="#流程控制" data-for="流程控制">流程控制</a></li><li class="depth-1"><a href="#条件语句" data-for="条件语句">条件语句</a></li><li class="depth-1"><a href="#循环语句" data-for="循环语句">循环语句</a></li><li class="depth-0"><a href="#脚本编写" data-for="脚本编写">脚本编写</a></li><li class="depth-1"><a href="#脚本参数和参数化脚本" data-for="脚本参数和参数化脚本">脚本参数和参数化脚本</a></li><li class="depth-1"><a href="#注释和文档" data-for="注释和文档">注释和文档</a></li><li class="depth-1"><a href="#示例代码" data-for="示例代码">示例代码</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&quot;path&quot;:&quot;Personal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2018&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2020&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2021&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2022&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Person
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>