doc: update docs/powershell.md #845 3662bb703c

This commit is contained in:
jaywcjlove
2024-10-29 12:11:16 +00:00
parent f8ad849358
commit 5255ed253c
4 changed files with 72 additions and 166 deletions

View File

@ -2,128 +2,12 @@
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>## 基本命令
### 辅助命令
**_Powershell 的命令遵循动词-名词格式_**
一些常见的动词:
| 动词 | 描述 |
| ------- | ------------------------ |
| Get | 用于检索信息 |
| Set | 用于配置或更改设置 |
| New | 用于创建新对象实例 |
| Remove | 用于删除或移除项目 |
| Invoke | 用于执行特定的操作或动作 |
| Start | 用于启动进程或操作 |
| Stop | 用于停止或终止进程或操作 |
| Enable | 用于激活或启用功能 |
| Disable | 用于停用或禁用功能 |
| Test | 用于执行测试或检查 |
| Update | 用于更新或刷新数据或配置 |
列出可用模块
```powershell
Get-Module --ListAvailable
```
列出可用的 cmdlet 和函数
```powershell
Get-Command -Module ActiveDirectory
```
获取帮助
```powershell
Get-Help &#x3C;cmd>
Get-Help &#x3C;cmd> -Examples
Get-Help -Name Get-Process -Parameter Id
```
列出别名及其对应的 cmdlet 名称
```powershell
Get-Alias | Select-Object Name, Definition
```
**Get-Member:** 显示对象的属性和方法
```powershell
Get-Process | Get-Member
```
### 对象操作
**Select-Object:** 选择对象的特定属性或自定义其显示
```powershell
Get-Process | Select-Object Name, CPU
```
**Where-Object:** 根据指定条件过滤对象
```powershell
Get-Service | Where-Object { $PSItem.Status -eq 'Running' }
#OR
Get-Service | ? { $_.Status -eq 'Running' }
```
**Measure-Object:** 计算对象属性的统计信息,如总和、平均值和计数
```powershell
Get-Process | Measure-Object -Property WorkingSet -Sum
```
**ForEach-Object:** 对集合中的每个对象执行操作(注意:以下命令将为当前目录中的文件/文件夹添加前缀)
```powershell
Get-ChildItem | ForEach-Object { Rename-Item $_ -NewName "Prefix_$_" }
```
**Sort-Object:** 按指定属性对对象进行排序
```powershell
Get-ChildItem | Sort-Object Length -Descending
```
**Format-Table:** 将输出格式化为带有指定列的表格
```powershell
Get-Service | Format-Table -AutoSize # ft alias
```
**Format-List:** 将输出格式化为属性和值的列表
```powershell
Get-Process | Format-List -Property Name, CPU # fl alias
```
### 文件系统
```powershell
New-Item -path file.txt -type 'file' -value 'contents'
New-Item -path file.txt -type 'dir'
Copy-Item &#x3C;src> -destination &#x3C;dest>
Move-Item -path &#x3C;src> -destination &#x3C;dest>
Remove-Item &#x3C;file>
Test-Path &#x3C;path>
Rename-Item -path &#x3C;path> -newname &#x3C;newname>
# using .NET Base Class Library
[System.IO.File]::WriteAllText('test.txt', '')
[System.IO.File]::Delete('test.txt')
Get-Content -Path "test.txt"
Get-Process | Out-File -FilePath "processes.txt"# 输出到文件
Get-Process | Export-Csv -Path "processes.csv" # 输出到 CSV
$data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
<title>PowerShell 备忘清单
&#x26; powershell cheatsheet &#x26; Quick Reference</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta description=",为开发人员分享快速参考备忘单。">
<meta description="一种强大的命令行界面和脚本语言,主要用于自动化任务和配置管理,特别适合系统管理员和 IT 专业人士。以下是 PowerShell 常用命令的备忘清单,可帮助快速参考常用操作。
常用操作,为开发人员分享快速参考备忘单。">
<meta keywords="powershell,reference,Quick,Reference,cheatsheet,cheat,sheet">
<meta name="author" content="jaywcjlove">
<meta name="license" content="MIT">
@ -150,10 +34,30 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
<path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.8.0"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="基本命令"><a aria-hidden="true" tabindex="-1" href="#基本命令"><span class="icon icon-link"></span></a>基本命令</h2><div class="wrap-body">
</button><script src="../js/dark.js?v=1.8.0"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="powershell-备忘清单"><svg viewBox="0 0 256 256" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
<rect width="256" height="256" rx="60" fill="#242938"></rect>
<path fill-rule="evenodd" clip-rule="evenodd" d="M36.407 203.248a8.242 8.242 0 0 1-6.627-3.021 8.81 8.81 0 0 1-1.5-7.512L58.252 61.787a12.855 12.855 0 0 1 12.004-9.784h149.338a8.259 8.259 0 0 1 6.627 3.021 8.815 8.815 0 0 1 1.5 7.513l-29.993 130.935a12.855 12.855 0 0 1-12.005 9.783H36.406v-.007Z" fill="url(#a)"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M70.256 53.686h149.338a6.516 6.516 0 0 1 5.445 2.553 6.52 6.52 0 0 1 1.039 5.924L196.085 193.09a11.252 11.252 0 0 1-10.362 8.476H36.406a6.51 6.51 0 0 1-5.445-2.553 6.52 6.52 0 0 1-1.04-5.923L59.916 62.163a11.24 11.24 0 0 1 10.34-8.477Z" fill="url(#b)"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M128.286 167.219h36.32a7.986 7.986 0 0 1 7.647 7.977 7.985 7.985 0 0 1-7.647 7.977h-36.32a7.986 7.986 0 0 1-7.647-7.977 7.985 7.985 0 0 1 7.647-7.977Zm23.56-35.463a12.576 12.576 0 0 1-4.221 4.285l-67.406 48.382a8.739 8.739 0 0 1-11.835-2.186 8.74 8.74 0 0 1 1.566-11.932l60.786-44.019v-.9L92.545 84.781a8.74 8.74 0 0 1 12.725-11.94l45.833 48.753a8.329 8.329 0 0 1 .743 10.162Z" fill="#2C5591"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M150.138 130.071a12.5 12.5 0 0 1-4.213 4.285L78.54 182.745a8.748 8.748 0 0 1-10.276-14.119l60.793-44.018v-.907L90.866 83.074a8.741 8.741 0 0 1 12.733-11.94l45.825 48.753a8.295 8.295 0 0 1 .714 10.184Zm-22.851 36.207h36.32a7.565 7.565 0 0 1 7.563 7.562 7.564 7.564 0 0 1-7.563 7.563h-36.32a7.564 7.564 0 0 1 0-15.125Z" fill="#fff"></path>
<defs>
<linearGradient id="a" x1="180.31" y1="174.355" x2="65.586" y2="71.877" gradientUnits="userSpaceOnUse">
<stop stop-color="#5391FE"></stop>
<stop offset="1" stop-color="#3E6DBF"></stop>
</linearGradient>
<linearGradient id="b" x1="64.443" y1="70.511" x2="177.996" y2="172.56" gradientUnits="userSpaceOnUse">
<stop stop-color="#5391FE"></stop>
<stop offset="1" stop-color="#3E6DBF"></stop>
</linearGradient>
</defs>
</svg>
<a aria-hidden="true" tabindex="-1" href="#powershell-备忘清单"><span class="icon icon-link"></span></a>PowerShell 备忘清单</h1><div class="wrap-body">
<p>一种强大的命令行界面和脚本语言,主要用于自动化任务和配置管理,特别适合系统管理员和 IT 专业人士。以下是 PowerShell 常用命令的备忘清单,可帮助快速参考常用操作。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
<path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#常用操作">常用操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#辅助命令">辅助命令</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#对象操作">对象操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#文件系统">文件系统</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#系统管理">系统管理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取信息">获取信息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#命名空间--类">命名空间 &#x26;</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#网络管理">网络管理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#用户和组管理">用户和组管理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#安全性和权限">安全性和权限</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#注册表管理">注册表管理</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#脚本">脚本</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符">运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#structure">Structure</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#输入输出操作">输入输出操作</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#流控制">流控制</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#函数--模块">函数 / 模块</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#示例-1">示例 1</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#示例-2">示例 2</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#模块">模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#注意">注意</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#参考">参考</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="常用操作"><a aria-hidden="true" tabindex="-1" href="#常用操作"><span class="icon icon-link"></span></a>常用操作</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="辅助命令"><a aria-hidden="true" tabindex="-1" href="#辅助命令"><span class="icon icon-link"></span></a>辅助命令</h3><div class="wrap-body">
<p><strong><em>Powershell 的命令遵循动词-名词格式</em></strong></p>
<p>一些常见的动词:</p>
<p><strong><em>PowerShell 的命令遵循动词-名词格式</em></strong> 一些常见的动词:</p>
@ -209,48 +113,48 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
<table><thead><tr><th>动词</th><th>描述</th></tr></thead><tbody><tr><td>Get</td><td>用于检索信息</td></tr><tr><td>Set</td><td>用于配置或更改设置</td></tr><tr><td>New</td><td>用于创建新对象实例</td></tr><tr><td>Remove</td><td>用于删除或移除项目</td></tr><tr><td>Invoke</td><td>用于执行特定的操作或动作</td></tr><tr><td>Start</td><td>用于启动进程或操作</td></tr><tr><td>Stop</td><td>用于停止或终止进程或操作</td></tr><tr><td>Enable</td><td>用于激活或启用功能</td></tr><tr><td>Disable</td><td>用于停用或禁用功能</td></tr><tr><td>Test</td><td>用于执行测试或检查</td></tr><tr><td>Update</td><td>用于更新或刷新数据或配置</td></tr></tbody></table>
<p>列出可用模块</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Module</span> <span class="token operator">--</span>ListAvailable
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Module</span> <span class="token operator">--</span>ListAvailable
</span></code></pre>
<p>列出可用的 cmdlet 和函数</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Command</span> <span class="token operator">-</span>Module ActiveDirectory
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Command</span> <span class="token operator">-</span>Module ActiveDirectory
</span></code></pre>
<p>列出别名及其对应的 cmdlet 名称</p>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Alias</span> <span class="token punctuation">|</span> <span class="token function">Select-Object</span> Name<span class="token punctuation">,</span> Definition
</span></code></pre>
<p>获取帮助</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Help</span> &#x3C;cmd>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Help</span> &#x3C;cmd>
</span><span class="code-line"><span class="token function">Get-Help</span> &#x3C;cmd> <span class="token operator">-</span>Examples
</span><span class="code-line"><span class="token function">Get-Help</span> <span class="token operator">-</span>Name <span class="token function">Get-Process</span> <span class="token operator">-</span>Parameter Id
</span></code></pre>
<p>列出别名及其对应的 cmdlet 名称</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Alias</span> <span class="token punctuation">|</span> <span class="token function">Select-Object</span> Name<span class="token punctuation">,</span> Definition
</span></code></pre>
<p><strong>Get-Member:</strong> 显示对象的属性和方法</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Process</span> <span class="token punctuation">|</span> <span class="token function">Get-Member</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Process</span> <span class="token punctuation">|</span> <span class="token function">Get-Member</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="对象操作"><a aria-hidden="true" tabindex="-1" href="#对象操作"><span class="icon icon-link"></span></a>对象操作</h3><div class="wrap-body">
<p><strong>Select-Object:</strong> 选择对象的特定属性或自定义其显示</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Process</span> <span class="token punctuation">|</span> <span class="token function">Select-Object</span> Name<span class="token punctuation">,</span> CPU
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Process</span> <span class="token punctuation">|</span> <span class="token function">Select-Object</span> Name<span class="token punctuation">,</span> CPU
</span></code></pre>
<p><strong>Where-Object:</strong> 根据指定条件过滤对象</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Service</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$PSItem</span><span class="token punctuation">.</span>Status <span class="token operator">-eq</span> <span class="token string">'Running'</span> <span class="token punctuation">}</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Service</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$PSItem</span><span class="token punctuation">.</span>Status <span class="token operator">-eq</span> <span class="token string">'Running'</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">#OR</span>
</span><span class="code-line"><span class="token function">Get-Service</span> <span class="token punctuation">|</span> ? <span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>Status <span class="token operator">-eq</span> <span class="token string">'Running'</span> <span class="token punctuation">}</span>
</span></code></pre>
<p><strong>Measure-Object:</strong> 计算对象属性的统计信息,如总和、平均值和计数</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Process</span> <span class="token punctuation">|</span> <span class="token function">Measure-Object</span> <span class="token operator">-</span>Property WorkingSet <span class="token operator">-</span>Sum
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Process</span> <span class="token punctuation">|</span> <span class="token function">Measure-Object</span> <span class="token operator">-</span>Property WorkingSet <span class="token operator">-</span>Sum
</span></code></pre>
<p><strong>ForEach-Object:</strong> 对集合中的每个对象执行操作(注意:以下命令将为当前目录中的文件/文件夹添加前缀)</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-ChildItem</span> <span class="token punctuation">|</span> <span class="token function">ForEach-Object</span> <span class="token punctuation">{</span> <span class="token function">Rename-Item</span> <span class="token variable">$_</span> <span class="token operator">-</span>NewName <span class="token string">"Prefix_<span class="token variable">$_</span>"</span> <span class="token punctuation">}</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-ChildItem</span> <span class="token punctuation">|</span> <span class="token function">ForEach-Object</span> <span class="token punctuation">{</span> <span class="token function">Rename-Item</span> <span class="token variable">$_</span> <span class="token operator">-</span>NewName <span class="token string">"Prefix_<span class="token variable">$_</span>"</span> <span class="token punctuation">}</span>
</span></code></pre>
<p><strong>Sort-Object:</strong> 按指定属性对对象进行排序</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-ChildItem</span> <span class="token punctuation">|</span> <span class="token function">Sort-Object</span> Length <span class="token operator">-</span>Descending
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-ChildItem</span> <span class="token punctuation">|</span> <span class="token function">Sort-Object</span> Length <span class="token operator">-</span>Descending
</span></code></pre>
<p><strong>Format-Table:</strong> 将输出格式化为带有指定列的表格</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Service</span> <span class="token punctuation">|</span> <span class="token function">Format-Table</span> <span class="token operator">-</span>AutoSize <span class="token comment"># ft alias</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Service</span> <span class="token punctuation">|</span> <span class="token function">Format-Table</span> <span class="token operator">-</span>AutoSize <span class="token comment"># ft alias</span>
</span></code></pre>
<p><strong>Format-List:</strong> 将输出格式化为属性和值的列表</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">Get-Process</span> <span class="token punctuation">|</span> <span class="token function">Format-List</span> <span class="token operator">-</span>Property Name<span class="token punctuation">,</span> CPU <span class="token comment"># fl alias</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">Get-Process</span> <span class="token punctuation">|</span> <span class="token function">Format-List</span> <span class="token operator">-</span>Property Name<span class="token punctuation">,</span> CPU <span class="token comment"># fl alias</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="文件系统"><a aria-hidden="true" tabindex="-1" href="#文件系统"><span class="icon icon-link"></span></a>文件系统</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token function">New-Item</span> <span class="token operator">-</span>path file<span class="token punctuation">.</span>txt <span class="token operator">-</span><span class="token function">type</span> <span class="token string">'file'</span> <span class="token operator">-</span>value <span class="token string">'contents'</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">New-Item</span> <span class="token operator">-</span>path file<span class="token punctuation">.</span>txt <span class="token operator">-</span><span class="token function">type</span> <span class="token string">'file'</span> <span class="token operator">-</span>value <span class="token string">'contents'</span>
</span><span class="code-line"><span class="token function">New-Item</span> <span class="token operator">-</span>path file<span class="token punctuation">.</span>txt <span class="token operator">-</span><span class="token function">type</span> <span class="token string">'dir'</span>
</span><span class="code-line"><span class="token function">Copy-Item</span> &#x3C;src> <span class="token operator">-</span>destination &#x3C;dest>
</span><span class="code-line"><span class="token function">Move-Item</span> <span class="token operator">-</span>path &#x3C;src> <span class="token operator">-</span>destination &#x3C;dest>
@ -268,7 +172,8 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span><span class="code-line"><span class="token variable">$data</span> = <span class="token function">Import-Csv</span> <span class="token operator">-</span>Path <span class="token string">"data.csv"</span> <span class="token comment"># 从 CSV 导入</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="系统管理"><a aria-hidden="true" tabindex="-1" href="#系统管理"><span class="icon icon-link"></span></a>系统管理</h2><div class="wrap-body">
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token comment"># 获取 BIOS 信息</span>
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="获取信息"><a aria-hidden="true" tabindex="-1" href="#获取信息"><span class="icon icon-link"></span></a>获取信息</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token comment"># 获取 BIOS 信息</span>
</span><span class="code-line"><span class="token function">Get-CimInstance</span> <span class="token operator">-</span>ClassName Win32_BIOS
</span><span class="code-line"><span class="token comment"># 获取本地连接的物理磁盘设备信息</span>
</span><span class="code-line"><span class="token function">Get-CimInstance</span> <span class="token operator">-</span>ClassName Win32_DiskDrive
@ -278,16 +183,17 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span><span class="code-line"><span class="token function">Get-CimInstance</span> <span class="token operator">-</span>ClassName Win32_NetworkAdapter
</span><span class="code-line"><span class="token comment"># 获取安装的图形/显卡GPU信息</span>
</span><span class="code-line"><span class="token function">Get-CimInstance</span> <span class="token operator">-</span>ClassName Win32_VideoController
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 列出所有类名</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="命名空间--类"><a aria-hidden="true" tabindex="-1" href="#命名空间--类"><span class="icon icon-link"></span></a>命名空间 &#x26;</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token comment"># 列出所有类名</span>
</span><span class="code-line"><span class="token function">Get-CimClass</span> <span class="token punctuation">|</span> <span class="token function">Select-Object</span> <span class="token operator">-</span>ExpandProperty CimClassName
</span><span class="code-line"><span class="token comment"># 探索 root\cimv2 命名空间中的各种 WMI 类</span>
</span><span class="code-line"><span class="token function">Get-CimClass</span> <span class="token operator">-</span>Namespace root\cimv2
</span><span class="code-line"><span class="token comment"># 探索 root\cimv2 命名空间下的子 WMI 命名空间</span>
</span><span class="code-line"><span class="token function">Get-CimInstance</span> <span class="token operator">-</span>Namespace root <span class="token operator">-</span>ClassName __NAMESPACE
</span></code></pre>
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="网络管理"><a aria-hidden="true" tabindex="-1" href="#网络管理"><span class="icon icon-link"></span></a>网络管理</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token comment"># 测试与远程主机的网络连接</span>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="网络管理"><a aria-hidden="true" tabindex="-1" href="#网络管理"><span class="icon icon-link"></span></a>网络管理</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token comment"># 测试与远程主机的网络连接</span>
</span><span class="code-line"><span class="token function">Test-Connection</span> <span class="token operator">-</span>ComputerName google<span class="token punctuation">.</span>com
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 获取网络适配器信息</span>
@ -304,7 +210,7 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span><span class="code-line">
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="用户和组管理"><a aria-hidden="true" tabindex="-1" href="#用户和组管理"><span class="icon icon-link"></span></a>用户和组管理</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token comment"># 获取本地用户账户信息</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token comment"># 获取本地用户账户信息</span>
</span><span class="code-line"><span class="token function">Get-LocalUser</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 创建新的本地用户账户</span>
@ -320,14 +226,14 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span><span class="code-line"><span class="token function">Add-LocalGroupMember</span> <span class="token operator">-</span><span class="token function">Group</span> Administrators <span class="token operator">-</span>Member UserToAdd
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="安全性和权限"><a aria-hidden="true" tabindex="-1" href="#安全性和权限"><span class="icon icon-link"></span></a>安全性和权限</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token comment"># 获取文件/目录的访问控制列表</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token comment"># 获取文件/目录的访问控制列表</span>
</span><span class="code-line"><span class="token function">Get-Acl</span> C:\Path\To\File<span class="token punctuation">.</span>txt
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 设置文件/目录的访问控制列表</span>
</span><span class="code-line"><span class="token function">Set-Acl</span> <span class="token operator">-</span>Path C:\Path\To\File<span class="token punctuation">.</span>txt <span class="token operator">-</span>AclObject <span class="token variable">$aclObject</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="注册表管理"><a aria-hidden="true" tabindex="-1" href="#注册表管理"><span class="icon icon-link"></span></a>注册表管理</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token comment"># 获取注册表键值</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token comment"># 获取注册表键值</span>
</span><span class="code-line"><span class="token function">Get-ItemProperty</span> <span class="token operator">-</span>Path <span class="token string">"HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"</span> <span class="token punctuation">|</span> <span class="token function">Select</span> DisplayName<span class="token punctuation">,</span> DisplayVersion
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 设置注册表键值</span>
@ -345,7 +251,7 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="脚本"><a aria-hidden="true" tabindex="-1" href="#脚本"><span class="icon icon-link"></span></a>脚本</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量"><a aria-hidden="true" tabindex="-1" href="#变量"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<p>初始化变量,指定或不指定类型:</p>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token variable">$var</span> = 0
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token variable">$var</span> = 0
</span><span class="code-line"><span class="token namespace">[int]</span> <span class="token variable">$var</span> = <span class="token string">'Trevor'</span> <span class="token comment"># (抛出异常)</span>
</span><span class="code-line"><span class="token namespace">[string]</span> <span class="token variable">$var</span> = <span class="token string">'Trevor'</span> <span class="token comment"># (不会抛出异常)</span>
</span><span class="code-line"><span class="token variable">$var</span><span class="token punctuation">.</span>GetType<span class="token punctuation">(</span><span class="token punctuation">)</span>
@ -360,7 +266,7 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span><span class="code-line"><span class="token variable">$dict</span> = @<span class="token punctuation">{</span>k1 = <span class="token string">'test'</span><span class="token punctuation">;</span> k2 = <span class="token string">'best'</span><span class="token punctuation">}</span>
</span></code></pre>
<p>变量命令</p>
<pre class="language-powershell"><code class="language-Powershell code-highlight"><span class="code-line"><span class="token function">New-Variable</span> <span class="token operator">-</span>Name FirstName <span class="token operator">-</span>Value Trevor
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token function">New-Variable</span> <span class="token operator">-</span>Name FirstName <span class="token operator">-</span>Value Trevor
</span><span class="code-line"><span class="token function">New-Variable</span> FirstName <span class="token operator">-</span>Value Trevor <span class="token operator">-</span>Option &#x3C;ReadOnly/Constant>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">Get-Variable</span>
@ -373,7 +279,7 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span></code></pre>
<p>变量类型int32, int64, string, bool</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="运算符"><a aria-hidden="true" tabindex="-1" href="#运算符"><span class="icon icon-link"></span></a>运算符</h3><div class="wrap-body">
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token comment"># 运算符</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token comment"># 运算符</span>
</span><span class="code-line"><span class="token comment"># (a &#x3C;op> b)</span>
</span><span class="code-line">
</span><span class="code-line">= <span class="token punctuation">,</span> <span class="token operator">+=</span> <span class="token operator">/</span> <span class="token operator">-=</span> <span class="token punctuation">,</span> <span class="token operator">++</span> <span class="token operator">/</span> <span class="token operator">--</span>
@ -402,7 +308,7 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="structure"><a aria-hidden="true" tabindex="-1" href="#structure"><span class="icon icon-link"></span></a>Structure</h3><div class="wrap-body">
<h4 id="输入输出操作"><a aria-hidden="true" tabindex="-1" href="#输入输出操作"><span class="icon icon-link"></span></a>输入输出操作</h4>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token string">"This displays a string"</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token string">"This displays a string"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">Write-Host</span> <span class="token string">"color"</span> <span class="token operator">-</span>ForegroundColor Red
</span><span class="code-line">
@ -413,11 +319,11 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span><span class="code-line"><span class="token function">Clear-Host</span>
</span></code></pre>
<h4 id="流控制"><a aria-hidden="true" tabindex="-1" href="#流控制"><span class="icon icon-link"></span></a>流控制</h4>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token keyword">IF</span><span class="token punctuation">(</span><span class="token comment">&#x3C;#Condition#></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token keyword">IF</span><span class="token punctuation">(</span><span class="token comment">&#x3C;#Condition#></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
</span><span class="code-line"><span class="token comment">&#x3C;#Commands#></span><span class="token punctuation">}</span><span class="token keyword">ELSEIF</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token keyword">ELSE</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">Switch</span><span class="token punctuation">(</span><span class="token variable">$var</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
</span><span class="code-line"> <span class="token string">"val1"</span><span class="token punctuation">{</span><span class="token comment">&#x3C;#Commands#></span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">}</span>
</span><span class="code-line"> <span class="token string">"val1"</span><span class="token punctuation">{</span><span class="token comment">&#x3C;#Commands#></span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">}</span>
</span><span class="code-line"> <span class="token string">"val2"</span><span class="token punctuation">{</span><span class="token comment">&#x3C;#Commands#></span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
@ -432,20 +338,20 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="函数--模块"><a aria-hidden="true" tabindex="-1" href="#函数--模块"><span class="icon icon-link"></span></a>函数 / 模块</h3><div class="wrap-body">
<h4 id="示例-1"><a aria-hidden="true" tabindex="-1" href="#示例-1"><span class="icon icon-link"></span></a>示例 1</h4>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token keyword">function</span> funcname<span class="token punctuation">{</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token keyword">function</span> funcname<span class="token punctuation">{</span>
</span><span class="code-line">
</span><span class="code-line"> <span class="token namespace">[CmdletBinding()]</span>
</span><span class="code-line"> <span class="token keyword">param</span><span class="token punctuation">(</span>
</span><span class="code-line"> <span class="token namespace">[Parameter(Mandatory)]</span>
</span><span class="code-line"> <span class="token namespace">[String]</span><span class="token variable">$user</span>
</span><span class="code-line"> <span class="token punctuation">)</span>
</span><span class="code-line"> <span class="token function">Write-Host</span> <span class="token string">"welcome "</span> <span class="token variable">$user</span>
</span><span class="code-line"> <span class="token keyword">param</span><span class="token punctuation">(</span>
</span><span class="code-line"> <span class="token namespace">[Parameter(Mandatory)]</span>
</span><span class="code-line"> <span class="token namespace">[String]</span><span class="token variable">$user</span>
</span><span class="code-line"> <span class="token punctuation">)</span>
</span><span class="code-line"> <span class="token function">Write-Host</span> <span class="token string">"welcome "</span> <span class="token variable">$user</span>
</span><span class="code-line"> <span class="token keyword">return</span> <span class="token string">"value"</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token variable">$var</span> = funcname <span class="token operator">-</span>user pcb
</span></code></pre>
<h4 id="示例-2"><a aria-hidden="true" tabindex="-1" href="#示例-2"><span class="icon icon-link"></span></a>示例 2</h4>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token keyword">function</span> <span class="token function">Get-EvenNumbers</span> <span class="token punctuation">{</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token keyword">function</span> <span class="token function">Get-EvenNumbers</span> <span class="token punctuation">{</span>
</span><span class="code-line"> <span class="token namespace">[CmdletBinding()]</span>
</span><span class="code-line"> <span class="token keyword">param</span> <span class="token punctuation">(</span>
</span><span class="code-line"> <span class="token namespace">[Parameter(ValueFromPipeline = $true)]</span>
@ -463,7 +369,7 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</span><span class="code-line">
</span></code></pre>
<h4 id="模块"><a aria-hidden="true" tabindex="-1" href="#模块"><span class="icon icon-link"></span></a>模块</h4>
<pre class="language-powershell"><code class="language-powershell code-highlight"><span class="code-line"><span class="token comment"># PowerShell 在路径中查找模块</span>
<pre class="language-powershell"><code class="language-PowerShell code-highlight"><span class="code-line"><span class="token comment"># PowerShell 在路径中查找模块</span>
</span><span class="code-line"><span class="token variable">$env</span>:PSModulePath
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 列出系统上安装的所有模块</span>
@ -488,10 +394,10 @@ $data &#x26; powershell cheatsheet &#x26; Quick Reference</title>
</ul>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="参考"><a aria-hidden="true" tabindex="-1" href="#参考"><span class="icon icon-link"></span></a>参考</h2><div class="wrap-body">
<ul>
<li><a href="https://learn.microsoft.com/en-us/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.3">Microsoft Powershell</a> <em>(learn.microsoft.com)</em></li>
<li><a href="https://learn.microsoft.com/en-us/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.3">Microsoft PowerShell</a> <em>(learn.microsoft.com)</em></li>
<li><a href="https://cheatsheets.zip/powershell">cheatsheets</a></li>
</ul>
</div></div><div class="h2wrap-body"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>.</footer></footer><script src="../data.js?v=1.8.0" defer></script><script src="../js/fuse.min.js?v=1.8.0" defer></script><script src="../js/main.js?v=1.8.0" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>.</footer></footer><script src="../data.js?v=1.8.0" defer></script><script src="../js/fuse.min.js?v=1.8.0" defer></script><script src="../js/main.js?v=1.8.0" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
<path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>