Reputation: 195
When I type in the dir command in Powershell, it always displays the current path + a few empty lines, which is really unnecessary. I would want to set an alias that removes the current path from the result. But what command/attribute can I use to remove that path? I couldn't find anything on the internet or the man page of dir.
I use Powershell 7.2.1 and am new to PS.
Upvotes: 2
Views: 996
Reputation: 60838
What you're seeing is the format definition of Get-ChildItem
, specifically the GroupBy
tag:
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
</GroupBy>
You can see how it's format definition looks like by exporting it to a file using Get-FormatData
to query the target TypeNames and then Export-FormatData
to store it in a file:
Get-FormatData -TypeName System.IO.FileInfo, System.IO.DirectoryInfo |
Export-FormatData -Path path\to\myCustomGetChildItemView.ps1xml
You can then update this ps1xml
file at your will and design your own custom view for these Types, in this case, we're targeting System.IO.FileInfo
and System.IO.Directory
. For this answer you can try this ps1xml
which should do the trick of getting rid of those empty lines and displaying the parent path.
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>customgciview</Name>
<ViewSelectedBy>
<TypeName>System.IO.DirectoryInfo</TypeName>
<TypeName>System.IO.FileInfo</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Mode</Label>
<Width>7</Width>
<Alignment>Left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>LastWriteTime</Label>
<Width>26</Width>
<Alignment>Right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Length</Label>
<Width>14</Width>
<Alignment>Right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Name</Label>
<Alignment>Left</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap />
<TableColumnItems>
<TableColumnItem>
<PropertyName>Mode</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>LastWriteTimeString</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>LengthString</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>NameString</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
After updating the file you can then use Update-FormatData
to update how these objects looks in your console:
Update-FormatData -PrependPath path\to\myCustomGetChildItemView.ps1xml
Upvotes: 2
Reputation: 2368
First of all, I'm thinking the mentioning of 7.2.1 is scaring people away from trying to answer your question. Many, such as myself, are staying with 5.x for now. If I knew how to get 7.x onto WinPE, I'd probably make the switch. I could be wrong about this, but it seems to be a problem.
Second, Dir, in PowerShell, is an alias for Get-ChildItem. See this List of Compatibility Aliases
Third, you need to look at Working with Files and Folders, Get-ChildItem, and Get-Item.
Fourth, Everything in PowerShell is an object. So all the extra lines you see with Dir isn't actually created by Dir, they are formatting fluff that PowerShell sticks in there to try to make it readable. PowerShell took the objects returned by Dir/Get-ChildItem and tried to make them pretty for you, but all that extra fluff doesn't exist when you work with the objects directly. When you get into the use of the Pipeline, keep this in mind, it's just an array of objects being feed into the pipe one at a time.
Fifth, all version of PowerShell 5.x and newer have a fair amount of overlap, so in theory, if I'm carful, the 5.x code I'm giving you should work in 7.x. If I made a mistake, I guess sorry - I tried!
In this code:
# Uncomment only one of the following lines at a time:
#$Objects = Get-ChildItem -Path $Home -File # Gets files in home path
#$Objects = Get-ChildItem -Path $Home -Directory # Gets Directories in home path
#$Objects = Get-ChildItem -Path $PSScriptRoot -File # Gets files in same folder as the script
#$Objects = Get-ChildItem -Path $PSScriptRoot -Directory # Gets Directories in same folder as the script
$Objects = Get-ChildItem -Path "$Home\Documents" -File
#$Objects = Get-ChildItem -Path "$PSScriptRoot\*.ps1" -File
#$Objects = Get-Item -Path "$PSScriptRoot\*.ps1"
$Objects | ForEach-Object { # Get files
#$_ | Format-List -Property *
$f =$_.FullName # Get full path name
$d = "$($_.PSDrive):\" # Get the drive
$dp = $_.DirectoryName # Get drive and path
$p = $dp.SubString($d.Length) # Get path only
$n = $_.BaseName # Get file name only
$x = $_.Extension # Get file extension only
$nx = $_.Name # Get files name and extension
Write-Host
Write-Host "f: $f"
Write-Host "dp: $dp, nx: $nx"
Write-Host "d: $d, p: $p, n: $n, x: $x"
}
Upvotes: 2