支持所有Powershell版本。
当你克隆了对象你就可以修改它所有的属性。克隆对象用来拆分重组原始数据是一个不错的方法。一旦你克隆了对象,就可以任意操作这个对象了。例如,更改或调整它的属性。
克隆对象,通过Select-Object操作它们。就这些。
这个例子使用了一份文件列表,通过运行Select-Object修饰了部分数据:
Get-ChildItem -Path c:\windows |
# clone the objects and keep the properties you want/add new properties (like "age...")
Select-Object -Property LastWriteTime, 'Age(days)', Length, Name, PSIsContainer |
# change the properties of the cloned object as you like
ForEach-Object {
# calculate the file/folder age in days
$_.'Age(days)' = (New-Timespan -Start $_.LastWriteTime).Days
# if it is a file, change size in bytes to size in MB
if ($_.PSisContainer -eq $false)
{
$_.Length = ('{0:N1} MB' -f ($_.Length / 1MB))
}
# do not forget to return the adjusted object so the next one gets it
$_
} |
# finally, select the properties you want in your report:
Select-Object -Property LastWriteTime, 'Age(days)', Length, Name |
# sort them as you like:
Sort-Object -Property LastWriteTime -Descending |
Out-GridView
相比原来的字节现在文件使用兆来显示了大小,同时创建了一个名为“年龄(天)”的新列 用来显示文件和文件夹的创建天数。
本文链接: https://www.pstips.net/creating-great-reports.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
