Home > Mobile >  How to remove current path from dir command in PowerShell?
How to remove current path from dir command in PowerShell?

Time:02-21

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.

CodePudding user response:

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:

  1. Take turns commenting out, and uncommenting, the "Objects =" lines near the top.
  2. Take note of the Commented out "$_ | Format-List -Property *". If you uncomment it, it will produce a long output of all the properties in the objects being fed into the pipeline. You can use that to see how I'm mostly accessing those objects to set the variables.
  3. Take note of the use of SubString in the code. I'm having a hard time proving SubString is available in PowerShell core, but if it is, it is a tools that can be used to brake a path into parts. Another tool is Split-Path, so you may want to look into that.
  4. If you have an exact name of single file you want, then in the code below replace the *.ps1 with that exact name. Or in many of the commands adding \FileName.ext to the end of the path will work.
  5. This code works well in Windows, but in another OS you will probably have to make adjustments.
#   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"
}

  • Related