Powershell将get-physicaldisk打印到文本文件

时间:2019-02-02 22:54:57

标签: powershell

我需要获取我们公司计算机使用的所有硬盘的列表。我认为最好的方法是运行一个启动脚本,将信息输出到文本文件。

我遇到的问题是打印到文本文件时。 get-physicaldisk可在控制台中使用,但是在打印到文本文件时会打印出不同的内容。

$name = $env:COMPUTERNAME
$disk = get-physicaldisk | format-table -Auto deviceID,Size,BusType,MediaType,Model

New-Item -ItemType "file" -Path . -Name $name'.txt' -Value "$name `r`n$disk" -Force

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

您要将输出从Get-PhysicalDisk重定向到文件:

$name = $env:COMPUTERNAME
$output_file_path = '~\temp\drives.txt'

Get-PhysicalDisk |
 Format-Table -Auto DeviceID, Size, BusType, MediaType, Model |
 Out-File -FilePath $output_file_path -Encoding 'ASCII' -Append

您可能需要将Format-Table替换为ExportTo-CSV,否则以后将很有趣地处理所有输出。

答案 1 :(得分:1)

您可以将产品作为字符串输出。通过管道输送到| out-string

$name = $env:COMPUTERNAME
$disk = get-physicaldisk | format-table -Auto deviceID,Size,BusType,MediaType,Model | out-string
New-Item -ItemType "file" -Path . -Name $name'.txt' -Value "$name `r`n$($disk.ToString())" -Force