[玩转系统] 使用格式命令更改输出视图
作者:精品下载站 日期:2024-12-14 03:02:23 浏览:14 分类:玩电脑
使用格式命令更改输出视图
PowerShell 具有一组 cmdlet,可让您控制如何显示特定对象的属性。所有 cmdlet 的名称均以动词 Format
开头。它们允许您选择要显示的属性。
Get-Command -Verb Format -Module Microsoft.PowerShell.Utility
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Format-Custom 6.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-Hex 6.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-List 6.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-Table 6.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-Wide 6.1.0.0 Microsoft.PowerShell.Utility
本文介绍Format-Wide
、Format-List
和Format-Table
cmdlet。
PowerShell 中的每种对象类型都有默认属性,当您未选择要显示的属性时,将使用这些默认属性。每个 cmdlet 使用相同的 Property 参数来指定要显示的属性。由于Format-Wide
仅显示单个属性,因此其Property参数仅采用单个值,但Format-List
和Format-Table 接受属性名称列表。
在此示例中,Get-Process
cmdlet 的默认输出显示我们正在运行两个 Internet Explorer 实例。
Get-Process -Name iexplore
Process 对象的默认格式显示此处显示的属性:
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
32 25.52 10.25 13.11 12808 1 iexplore
52 11.46 26.46 3.55 21748 1 iexplore
使用 Format-Wide 进行单项输出
默认情况下,Format-Wide
cmdlet 仅显示对象的默认属性。与每个对象关联的信息显示在单个列中:
Get-Command -Verb Format | Format-Wide
Format-Custom Format-Hex
Format-List Format-Table
Format-Wide
您还可以指定非默认属性:
Get-Command -Verb Format | Format-Wide -Property Noun
Custom Hex
List Table
Wide
使用列控制全格式显示
使用 Format-Wide
cmdlet,您一次只能显示一个属性。这使得它对于在多列中显示大型列表非常有用。
Get-Command -Verb Format | Format-Wide -Property Noun -Column 3
Custom Hex List
Table Wide
使用 Format-List 进行列表视图
Format-List
cmdlet 以列表的形式显示对象,每个属性都标记并显示在单独的行上:
Get-Process -Name iexplore | Format-List
Id : 12808
Handles : 578
CPU : 13.140625
SI : 1
Name : iexplore
Id : 21748
Handles : 641
CPU : 3.59375
SI : 1
Name : iexplore
您可以根据需要指定任意数量的属性:
Get-Process -Name iexplore | Format-List -Property ProcessName,FileVersion,StartTime,Id
ProcessName : iexplore
FileVersion : 11.00.18362.1 (WinBuild.160101.0800)
StartTime : 10/22/2019 11:23:58 AM
Id : 12808
ProcessName : iexplore
FileVersion : 11.00.18362.1 (WinBuild.160101.0800)
StartTime : 10/22/2019 11:23:57 AM
Id : 21748
使用带有通配符的 Format-List 获取详细信息
Format-List
cmdlet 允许您使用通配符作为其Property 参数的值。这使您可以显示详细信息。通常,对象包含的信息比您需要的更多,这就是为什么 PowerShell 默认情况下不显示所有属性值的原因。要显示对象的所有属性,请使用Format-List -Property *
命令。以下命令为单个进程生成 60 多行输出:
Get-Process -Name iexplore | Format-List -Property *
尽管 Format-List
命令对于显示详细信息非常有用,但如果您想要包含许多项目的输出概览,则更简单的表格视图通常更有用。
使用 Format-Table 进行表格输出
如果您使用未指定属性名称的 Format-Table
cmdlet 来格式化 Get-Process
命令的输出,则您将获得与不使用 代码>格式
cmdlet。默认情况下,PowerShell 以表格格式显示进程对象。
Get-Service -Name win* | Format-Table
Status Name DisplayName
------ ---- -----------
Running WinDefend Windows Defender Antivirus Service
Running WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
Running Winmgmt Windows Management Instrumentation
Running WinRM Windows Remote Management (WS-Manag...
笔记
Get-Service
仅适用于 Windows 平台。
改进格式表输出
尽管表格视图对于显示大量信息很有用,但如果显示对于数据来说太窄,则可能难以解释。在前面的示例中,输出被截断。如果您在运行 Format-Table
命令时指定 AutoSize 参数,PowerShell 将根据实际显示的数据计算列宽。这使得列可读。
Get-Service -Name win* | Format-Table -AutoSize
Status Name DisplayName
------ ---- -----------
Running WinDefend Windows Defender Antivirus Service
Running WinHttpAutoProxySvc WinHTTP Web Proxy Auto-Discovery Service
Running Winmgmt Windows Management Instrumentation
Running WinRM Windows Remote Management (WS-Management)
Format-Table
cmdlet 可能仍会截断数据,但它仅在屏幕末尾截断。除了最后显示的属性之外,属性的大小与其最长的数据元素正确显示所需的大小相同。
Get-Service -Name win* |
Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -AutoSize
Name Status StartType DisplayName DependentServi
ces
---- ------ --------- ----------- --------------
WinDefend Running Automatic Windows Defender Antivirus Service {}
WinHttpAutoProxySvc Running Manual WinHTTP Web Proxy Auto-Discovery Service {NcaSvc, iphl…
Winmgmt Running Automatic Windows Management Instrumentation {vmms, TPHKLO…
WinRM Running Automatic Windows Remote Management (WS-Management) {}
Format-Table
命令假定属性按重要性顺序列出。该 cmdlet 尝试完全显示最接近开头的属性。如果 Format-Table
命令无法显示所有属性,它会从显示中删除一些列。您可以在前面的示例中的 DependentServices 属性中看到此行为。
将格式表输出包装在列中
您可以使用 Wrap 参数强制将冗长的 Format-Table
数据在其显示列中换行。使用 Wrap 参数可能不会达到您的预期,因为如果您没有指定 AutoSize,它会使用默认设置:
Get-Service -Name win* |
Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -Wrap
Name Status StartType DisplayName DependentServi
ces
---- ------ --------- ----------- --------------
WinDefend Running Automatic Windows Defender Antivirus Service {}
WinHttpAutoProxySvc Running Manual WinHTTP Web Proxy Auto-Discovery Service {NcaSvc,
iphlpsvc}
Winmgmt Running Automatic Windows Management Instrumentation {vmms,
TPHKLOAD,
SUService,
smstsmgr…}
WinRM Running Automatic Windows Remote Management (WS-Management) {}
单独使用Wrap 参数并不会显着降低处理速度。但是,使用AutoSize来格式化大型目录结构的递归文件列表可能需要很长时间,并且在显示第一个输出项目之前会使用大量内存。
如果您不关心系统负载,则 AutoSize 与 Wrap 参数配合使用效果很好。初始列仍使用在一行上显示项目所需的宽度,但如有必要,最后一列将被换行。
笔记
当您首先指定最宽的列时,某些列可能不会显示。为了获得最佳结果,请首先指定最小的数据元素。
在下面的示例中,我们首先指定最宽的属性。
Get-Process -Name iexplore |
Format-Table -Wrap -AutoSize -Property FileVersion, Path, Name, Id
即使进行换行,最后的 Id 列也会被省略:
FileVersion Path Nam
e
----------- ---- ---
11.00.18362.1 (WinBuild.160101.0800) C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE iex
plo
re
11.00.18362.1 (WinBuild.160101.0800) C:\Program Files\Internet Explorer\iexplore.exe iex
plo
re
组织表输出
表格输出控制的另一个有用参数是GroupBy。尤其是较长的表格列表可能很难比较。 GroupBy 参数根据属性值对输出进行分组。例如,我们可以按 StartType 对服务进行分组,以便于检查,并从属性列表中省略 StartType 值:
Get-Service -Name win* | Sort-Object StartType | Format-Table -GroupBy StartType
StartType: Automatic
Status Name DisplayName
------ ---- -----------
Running WinDefend Windows Defender Antivirus Service
Running Winmgmt Windows Management Instrumentation
Running WinRM Windows Remote Management (WS-Managem…
StartType: Manual
Status Name DisplayName
------ ---- -----------
Running WinHttpAutoProxyS… WinHTTP Web Proxy Auto-Discovery Serv…
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag