# adjust the name of the module # code will list all commands shipped by that module # list of all modules: Get-Module -ListAvailable $ModuleName = "PrintManagement"
# these assemblies provide access to UI Add-Type-AssemblyName PresentationCore Add-Type-AssemblyName PresentationFramework
# create a new document $document = [System.Windows.Documents.FlowDocument]::new()
# create a new table with two columns (20%, 80% width) $table = [System.Windows.Documents.Table]::new() $column1 = [System.Windows.Documents.TableColumn]::new() $column1.Width = [System.Windows.GridLength]::new(20, [System.Windows.GridUnitType]::Star) $table.Columns.Add($column1) $column2 = [System.Windows.Documents.TableColumn]::new() $column2.Width = [System.Windows.GridLength]::new(80, [System.Windows.GridUnitType]::Star) $table.Columns.Add($column2)
# create a new rowgroup that will store the table rows $rowGroup = [System.Windows.Documents.TableRowGroup]::new()
# produce the command data to display in the table Get-Command-Module$moduleName | Get-Help | ForEach-Object { # get the information to be added to the table $name = $_.Name $synopsis = $_.Synopsis $description = $_.Description.Text -join' '
# create a new table row $row = [System.Windows.Documents.TableRow]::new()
# add a cell with the command name in bold: $cell = [System.Windows.Documents.TableCell]::new() $cell.Padding = [System.Windows.Thickness]::new(0,0,10,0) $para = [System.Windows.Documents.Paragraph]::new() $inline = [System.Windows.Documents.Run]::new($name) $inline.FontWeight = "Bold" $inline.FontSize = 12 $inline.FontFamily = "Segoe UI" $para.Inlines.Add($inline) $cell.AddChild($para) $row.AddChild($cell)
# add a second cell with the command synopsis $cell = [System.Windows.Documents.TableCell]::new() $para = [System.Windows.Documents.Paragraph]::new() $inline = [System.Windows.Documents.Run]::new($synopsis) $inline.FontSize = 12 $inline.FontFamily = "Segoe UI" $para.Inlines.Add($inline) $cell.AddChild($para) $row.AddChild($cell)
# add both cells to the table $rowGroup.AddChild($row)
# add a second table row than spans two columns and holds the # command description in a smaller font: $row = [System.Windows.Documents.TableRow]::new() $cell = [System.Windows.Documents.TableCell]::new() $cell.ColumnSpan = 2 # add a 20pt gap at the bottom to separate from next command $cell.Padding = [System.Windows.Thickness]::new(0,0,0,20) $para = [System.Windows.Documents.Paragraph]::new() $inline = [System.Windows.Documents.Run]::new($description) $inline.FontSize = 10 $inline.FontFamily = "Segoe UI" $para.Inlines.Add($inline) $cell.AddChild($para) $row.AddChild($cell) # add row to table: $rowGroup.AddChild($row) }
# add all collected table rows to the table, and add the table # to the document $table.AddChild($rowGroup) $document.AddChild($table)
# add a paginator that controls where pages end and new pages start: [System.Windows.Documents.IDocumentPaginatorSource]$paginator = $document
# create a print dialog to select the printer $printDialog = [System.Windows.Controls.PrintDialog]::new() # print dialog can not be shown in ISE due to threading issues # selecting a printer will work only when running this code in powershell.exe # else, the default printer is used try { $result = $printDialog.ShowDialog() if ($result-eq$false) { Write-Warning"User aborted." return } } catch {}
# make sure the document is not printed in multiple columns $document.PagePadding = [System.Windows.Thickness]::new(50) $document.ColumnGap = 0 $document.ColumnWidth = $printDialog.PrintableAreaWidth
# print the document $printDialog.PrintDocument($paginator.DocumentPaginator, "WPF-Printing from PowerShell")
Name Synopsis ------------ Add-Printer Adds a printer to the specified computer. Add-PrinterDriver Installs a printer driver on the specified computer. Add-PrinterPort Installs a printer port on the specified computer. Get-PrintConfiguration Gets the configuration information of a printer. Get-Printer Retrieves a list of printers installed on a computer. Get-PrinterDriver Retrieves the list of printer drivers installed on... Get-PrinterPort Retrieves a list of printer ports installed on the... Get-PrinterProperty Retrieves printer properties for the specified pri... Get-PrintJob Retrieves a list of print jobs in the specified pr... Read-PrinterNfcTag Reads information about printers from an NFC tag. Remove-Printer Removes a printer from the specified computer. Remove-PrinterDriver Deletes printer driver from the specified computer. Remove-PrinterPort Removes the specified printer port from the specif... Remove-PrintJob Removes a print job on the specified printer. ...
Name description --------------- Add-Printer {@{Text=The Add-Printer cmdlet adds a printer to a specified computer. Y... Add-PrinterDriver {@{Text=The Add-PrinterDriver cmdlet installs a printer driver on the sp... Add-PrinterPort {@{Text=The Add-PrinterPort cmdlet creates a printer port on the specifi... Get-PrintConfiguration {@{Text=The Get-PrintConfiguration cmdlet gets the configuration informa... Get-Printer {@{Text=The Get-Printer cmdlet retrieves a list of printers installed on... Get-PrinterDriver {@{Text=The Get-PrinterDriver cmdlet retrieves the list of printer drive... Get-PrinterPort {@{Text=The Get-PrinterPort cmdlet retrieves a list of printer ports tha... Get-PrinterProperty {@{Text=The Get-PrinterProperty cmdlet retrieves one or more printer pro... Get-PrintJob {@{Text=The Get-PrintJob cmdlet retrieves the current print jobs in the ... Read-PrinterNfcTag {@{Text=The Read-PrinterNfcTag cmdlet reads information about printers f... Remove-Printer {@{Text=The Remove-Printer cmdlet deletes a printer from the specified c... Remove-PrinterDriver {@{Text=The Remove-PrinterDriver cmdlet deletes a printer driver from th... Remove-PrinterPort {@{Text=The Remove-PrinterPort cmdlet removes the specified printer port... ...
# adjust the name of the module # code will list all commands shipped by that module # list of all modules: Get-Module -ListAvailable $ModuleName = "PrintManagement"
Name Description --------------- Add-Printer The Add-Printer cmdlet adds a printer to a specified ... Add-PrinterDriver The Add-PrinterDriver cmdlet installs a printer drive... Add-PrinterPort The Add-PrinterPort cmdlet creates a printer port on ... Get-PrintConfiguration The Get-PrintConfiguration cmdlet gets the configurat... Get-Printer The Get-Printer cmdlet retrieves a list of printers i... Get-PrinterDriver The Get-PrinterDriver cmdlet retrieves the list of pr... Get-PrinterPort The Get-PrinterPort cmdlet retrieves a list of printe... Get-PrinterProperty The Get-PrinterProperty cmdlet retrieves one or more ... Get-PrintJob The Get-PrintJob cmdlet retrieves the current print j... Read-PrinterNfcTag The Read-PrinterNfcTag cmdlet reads information about... Remove-Printer The Remove-Printer cmdlet deletes a printer from the ... Remove-PrinterDriver The Remove-PrinterDriver cmdlet deletes a printer dri... Remove-PrinterPort The Remove-PrinterPort cmdlet removes the specified p... Remove-PrintJob The Remove-PrintJob cmdlet removes a print job on the... Rename-Printer The Rename-Printer cmdlet renames the specified print... Restart-PrintJob The Restart-PrintJob cmdlet restarts a print job on t... Resume-PrintJob The Resume-PrintJob cmdlet resumes a suspended print ... Set-PrintConfiguration The Set-PrintConfiguration cmdlet sets the printer co... Set-Printer The Set-Printer cmdlet updates the configuration of t...
在前一个技能中,我们创建了 PowerShell 命令的速查表并且将他们转换为可被浏览器打开和打印的 HTML 报告。它可以正常工作,但是输出页面不是非常精致。只要加上一些 HTML 延时,您的命令列表就可以上黄金档啦,而且您还可以用这个例子中的技术来“美化”任何通过 PowerShell 的 ConvertTo-Html 创建的 HTML 表格:
Name Synopsis ------------ Disable-NetAdapter Disables a network adapter. Disable-NetAdapterBinding Disables a binding to a netw... Disable-NetAdapterChecksumOffload Disables the selected checks... Disable-NetAdapterEncapsulatedPacketTaskOffload Disables encapsulated packet... Disable-NetAdapterIPsecOffload Disables IPsec offload on th... Disable-NetAdapterLso Disables all LSO properties,... Disable-NetAdapterPacketDirect ... Disable-NetAdapterPowerManagement Disables specific power mana... Disable-NetAdapterQos Disables QoS on a network ad... Disable-NetAdapterRdma Disables RDMA on a network a... Disable-NetAdapterRsc Disables RSC on a network ad... Disable-NetAdapterRss Disables RSS on a network ad... Disable-NetAdapterSriov Disables SR-IOV on a network... Disable-NetAdapterVmq Disables the VMQ feature on ... Enable-NetAdapter Enables a network adapter. Enable-NetAdapterBinding Enables binding of a protoco... Enable-NetAdapterChecksumOffload Enables checksum offloads on... Enable-NetAdapterEncapsulatedPacketTaskOffload Enables encapsulated packet ... Enable-NetAdapterIPsecOffload Enables IPsec offload on the... Enable-NetAdapterLso Enables LSO properties, such... Enable-NetAdapterPacketDirect ... Enable-NetAdapterPowerManagement Enables specific power manag... Enable-NetAdapterQos Enables QoS on the network a... Enable-NetAdapterRdma Enables RDMA on a network ad... Enable-NetAdapterRsc Enables RSC on a network ada... Enable-NetAdapterRss Enables RSS on a network ada... Enable-NetAdapterSriov Enables SR-IOV on a network ... Enable-NetAdapterVmq Enables VMQ on a network ada... Get-NetAdapter Gets the basic network adapt... Get-NetAdapterAdvancedProperty Gets the advanced properties... Get-NetAdapterBinding Gets a list of bindings for ... Get-NetAdapterChecksumOffload Gets the various checksum of... Get-NetAdapterEncapsulatedPacketTaskOffload Gets the network adapters th... ...
Name Synopsis ------------ Add-Computer Add the local computer to a domain or workgroup. Add-Content Adds content to the specified items, such as adding word... Checkpoint-Computer Creates a system restore point on the local computer. Clear-Content Deletes the contents of an item, but does not delete the... Clear-EventLog Clears all entries from specified event logs on the loca... Clear-Item Clears the contents of an item, but does not delete the ... Clear-ItemProperty Clears the value of a property but does not delete the p... Clear-RecycleBin Complete-Transac... Commits the active transaction. Convert-Path Converts a path from a Windows PowerShell path to a Wind... Copy-Item Copies an item from one location to another. Copy-ItemProperty Copies a property and value from a specified location to... Debug-Process Debugs one or more processes running on the local computer. Disable-Computer... Disables the System Restore feature on the specified fil... Enable-ComputerR... Enables the System Restore feature on the specified file... Get-ChildItem Gets the items and child items in one or more specified ... Get-Clipboard Gets the current Windows clipboard entry. Get-ComputerInfo Gets a consolidated object of system and operating syste... Get-ComputerRest... Gets the restore points on the local computer. Get-Content Gets the content of the item at the specified location. Get-ControlPanel... Gets control panel items. Get-EventLog Gets the events in an event log, or a list of the event ... Get-HotFix Gets the hotfixes that have been applied to the local an... Get-Item Gets the item at the specified location. Get-ItemProperty Gets the properties of a specified item. Get-ItemProperty... Gets the value for one or more properties of a specified... Get-Location Gets information about the current working location or a...
在 Windows 10 中,PowerShell 可以通过 WIN + X 键(或右键单击开始菜单图标)启动。如果您的 Windows 10 打开的是过时的 cmd.exe,那么该快速做出改变了。
单击开始菜单图标(或按 WIN 键)。在开始菜单中,单击“齿轮”图标打开 Windows 设置。您现在可以看到一个有许多图标的,名为“Windows 设置”的窗口,在顶部有一个叫做“查找设置”的文本框。在文本框中输入 “PowerShell” 即可打开“使用 Windows + X 菜单时,将命令提示符替换为 Windows PowerShell”。
# reuse the session for as many queries as you like $sh = Get-CimInstance-ClassName Win32_Share -CimSession$session-Filter'Name="Admin$"' $se = Get-CimInstance-ClassName Win32_Service -CimSession$session
# remove the session at the end Remove-CimSession-CimSession$session