如何使用 PowerShell 檢索檔案內容?


要在 PowerShell 中檢索檔案的內容,你需要使用 Get-Content cmdlet。例如,我們將從特定位置檢索名為 Aliases.txt 的文字檔案的內容。

示例

Get-Content D:\Temp\aliases.txt

輸出

PS C:\WINDOWS\system32> Get-Content D:\Temp\aliases.txt
# Alias File
# Exported by : admin
# Date/Time : 26 January 2020 19:20:24
# Computer : DESKTOP-9435KM9
"foreach","ForEach-Object","","ReadOnly, AllScope"
"%","ForEach-Object","","ReadOnly, AllScope"
"where","Where-Object","","ReadOnly, AllScope"
"?","Where-Object","","ReadOnly, AllScope"
"ac","Add-Content","","ReadOnly, AllScope"
"clc","Clear-Content","","ReadOnly, AllScope"
"cli","Clear-Item","","ReadOnly, AllScope"
"clp","Clear-ItemProperty","","ReadOnly, AllScope"
"clv","Clear-Variable","","ReadOnly, AllScope"
"compare","Compare-Object","","ReadOnly, AllScope"
"cpi","Copy-Item","","ReadOnly, AllScope"
"cpp","Copy-ItemProperty","","ReadOnly, AllScope"
"cvpa","Convert-Path","","ReadOnly, AllScope"
"dbp","Disable-PSBreakpoint","","ReadOnly, AllScope"
"diff","Compare-Object","","ReadOnly, AllScope"
"ebp","Enable-PSBreakpoint","","ReadOnly, AllScope"
"epal","Export-Alias","","ReadOnly, AllScope"
"epcsv","Export-Csv","","ReadOnly, AllScope"
"fc","Format-Custom","","ReadOnly, AllScope"
"fl","Format-List","","ReadOnly, AllScope"
"ft","Format-Table","","ReadOnly, AllScope"
"fw","Format-Wide","","ReadOnly, AllScope"
"gal","Get-Alias","","ReadOnly, AllScope"
"gbp","Get-PSBreakpoint","","ReadOnly, AllScope"
"gc","Get-Content","","ReadOnly, AllScope"

你還可以檢索遠端位置的檔案內容。

例如,

Get-Content \Test-PC\Shared\Powershellaliases.txt

更新於: 16-Mar-2020

525 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.