답변:
이 시도
PS> gc c:\scripts\type\shrf.ps1xml
<Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<Name>FileSize</Name>
<GetScriptBlock>
switch($this.length) {
{ $_ -gt 1tb }
{ "{0:n2} TB" -f ($_ / 1tb) }
{ $_ -gt 1gb }
{ "{0:n2} GB" -f ($_ / 1gb) }
{ $_ -gt 1mb }
{ "{0:n2} MB " -f ($_ / 1mb) }
{ $_ -gt 1kb }
{ "{0:n2} KB " -f ($_ / 1Kb) }
default
{ "{0} B " -f $_}
}
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
PS> Update-TypeData -AppendPath c:\scripts\type\shrf.ps1xml -verbose
PS> get-childItem $env:windir | select Name,FileSize,length
PS> # you can paste this in your profile
PS>
PS3에서 동적 유형 데이터를 사용할 수도 있습니다.
PS> Update-TypeData -TypeName System.IO.FileInfo -MemberName FileSize -MemberType ScriptProperty -Value {
switch($this.length) {
{ $_ -gt 1tb }
{ "{0:n2} TB" -f ($_ / 1tb) }
{ $_ -gt 1gb }
{ "{0:n2} GB" -f ($_ / 1gb) }
{ $_ -gt 1mb }
{ "{0:n2} MB " -f ($_ / 1mb) }
{ $_ -gt 1kb }
{ "{0:n2} KB " -f ($_ / 1Kb) }
default
{ "{0} B " -f $_}
}
} -DefaultDisplayPropertySet Mode,LastWriteTime,FileSize,Name
Get-ChildItem
그냥 상자의이을 할 것이라고는
-DefaultDisplayPropertySet
일해야합니까?
먼저 다음 함수를 작성하십시오.
Function Format-FileSize() {
Param ([int]$size)
If ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
ElseIf ($size -gt 0) {[string]::Format("{0:0.00} B", $size)}
Else {""}
}
그런 다음 파이프의 출력 수 Get-ChildItem
를 통해 Select-Object
하고는 사용하는 계산 된 속성을 파일 크기를 포맷 :
Get-ChildItem | Select-Object Name, @{Name="Size";Expression={Format-FileSize($_.Length)}}
물론 PB 범위 이상의 크기를 설명하거나 필요에 따라 소수점 수를 변경하도록 기능을 향상시킬 수 있습니다.
Get-Variable profile
위치 참조 ).
$size
로 정의 int
되는, . 큰 파일로이 작업을 수행하려면, 정의 로 또는 . int32
$size
int64
uint64
Select-Object : Es wurde kein Positionsparameter gefunden, der das Argument "System.Collections.Hashtable" akzeptiert.
. 경로를 어떻게 지정할 수 있습니까? 사용 $pst= Get-ChildItem -Path $home_user -Filter *.pst -Recurse -File| Sort-Object Length -Descending | ForEach-Object{ $_.FullName}
합니다. 이것은 작동하지만 파일 크기는 없습니다.
walid toumi의 답변을 바탕으로 :
해야 할 단계 :
FileSize
-Property를 사용하여 고유 한 형식 파일을 만듭니다.$PROFILE
FileSize
-Property를 사용하여 고유 한 형식 파일을 만듭니다.나만의 Type-file을 만드십시오 : MyTypes.ps1xml
(내가 $Env:USERPROFILE\Documents\WindowsPowershell
바로 옆 에 넣습니다 $PROFILE
)
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<!-- Filesize converts the length to a human readable
format (kb, mb, gb, tb) -->
<Name>FileSize</Name>
<GetScriptBlock>
switch($this.length) {
{ $_ -gt 1tb }
{ "{0:n2} TB" -f ($_ / 1tb) ; break }
{ $_ -gt 1gb }
{ "{0:n2} GB" -f ($_ / 1gb) ; break }
{ $_ -gt 1mb }
{ "{0:n2} MB " -f ($_ / 1mb) ; break }
{ $_ -gt 1kb }
{ "{0:n2} KB " -f ($_ / 1Kb) ; break }
default
{ "{0} B " -f $_}
}
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
powershell-session에 새 속성을로드하십시오.
Update-TypeData -PrependPath $Env:USERPROFILE\Documents\WindowsPowershell\MyTypes.ps1xml
Get-ChildItem | Format-Table -Property Name, Length, FileSize
자신의 화일 형식 파일을 만들 : MyFileFormat.format.ps1xml
(다시 $Env:USERPROFILE\Documents\WindowsPowershell\
)
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<SelectionSets>
<SelectionSet>
<Name>FileSystemTypes</Name>
<Types>
<TypeName>System.IO.DirectoryInfo</TypeName>
<TypeName>System.IO.FileInfo</TypeName>
</Types>
</SelectionSet>
</SelectionSets>
<!-- ################ GLOBAL CONTROL DEFINITIONS ################ -->
<Controls>
<Control>
<Name>FileSystemTypes-GroupingFormat</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<Text AssemblyName="System.Management.Automation" BaseName="FileSystemProviderStrings" ResourceId="DirectoryDisplayGrouping"/>
<ExpressionBinding>
<ScriptBlock>
$_.PSParentPath.Replace("Microsoft.PowerShell.Core\FileSystem::", "")
</ScriptBlock>
</ExpressionBinding>
<NewLine/>
</CustomItem>
</Frame>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
</Controls>
<!-- ################ VIEW DEFINITIONS ################ -->
<ViewDefinitions>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Mode</Label>
<Width>7</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>LastWriteTime</Label>
<Width>25</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>FileSize</Label>
<Width>14</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader/>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap/>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Mode</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
[String]::Format("{0,10} {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>FileSize</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<ListControl>
<ListEntries>
<ListEntry>
<EntrySelectedBy>
<TypeName>System.IO.FileInfo</TypeName>
</EntrySelectedBy>
<ListItems>
<ListItem>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<PropertyName>FileSize</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CreationTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastWriteTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastAccessTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Mode</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LinkType</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Target</PropertyName>
</ListItem>
<ListItem>
<PropertyName>VersionInfo</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<PropertyName>CreationTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastWriteTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LastAccessTime</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Mode</PropertyName>
</ListItem>
<ListItem>
<PropertyName>LinkType</PropertyName>
</ListItem>
<ListItem>
<PropertyName>Target</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>children</Name>
<ViewSelectedBy>
<SelectionSetName>FileSystemTypes</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<WideControl>
<WideEntries>
<WideEntry>
<WideItem>
<PropertyName>Name</PropertyName>
</WideItem>
</WideEntry>
<WideEntry>
<EntrySelectedBy>
<TypeName>System.IO.DirectoryInfo</TypeName>
</EntrySelectedBy>
<WideItem>
<PropertyName>Name</PropertyName>
<FormatString>[{0}]</FormatString>
</WideItem>
</WideEntry>
</WideEntries>
</WideControl>
</View>
<View>
<Name>FileSecurityTable</Name>
<ViewSelectedBy>
<TypeName>System.Security.AccessControl.FileSystemSecurity</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Path</Label>
</TableColumnHeader>
<TableColumnHeader />
<TableColumnHeader>
<Label>Access</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>
split-path $_.Path -leaf
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Owner</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$_.AccessToString
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>FileSystemStream</Name>
<ViewSelectedBy>
<TypeName>Microsoft.PowerShell.Commands.AlternateStreamData</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>Filename</PropertyName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>20</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Width>10</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Stream</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Length</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
(원본은 거의 직접 사본입니다 $PSHOME\FileFormat.format.ps1xml
. 몇 번만 변경 Length
했습니다 FileSize
)
powershell 세션에서 새 형식을로드하십시오.
Update-FormatData -PrependPath $Env:USERPROFILE\Documents\WindowsPowershell\MyFileFormat.format.ps1xml
Get-ChildItem
$PROFILE
$PROFILE
모든 새 세션에서 변경 사항을로드하려면 이 행을 복사하십시오.
# local path to use in this script
$scriptpath = Split-Path -parent $MyInvocation.MyCommand.Definition
# custom types and formats
# currently only System.IO.FileInfo is changed
update-TypeData -PrependPath $scriptpath\MyTypes.ps1xml
update-FormatData -PrependPath $scriptpath\MyFileFormat.format.ps1xml
Update-TypeData : Error in TypeData "System.IO.FileInfo": The member DefaultDisplayPropertySet is already present.
는 9/4에서 최신 PS3 정식 버전을 실행하는 것입니다.