split output fullname

Welcome Forums General PowerShell Q&A split output fullname

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 year, 3 months ago.

  • Author
    Posts
  • #73948

    Participant
    Points: 0
    Rank: Member

    Hi all,

    I am trying to remove certain folders from the output fullname path.
    In short, instead of H:/main/demo/first/ I would like something like this, /main/demo/first

    Here is the code I am working with

     Function Get-Depth {
        Param(
            [String]$Path = 'H:\main\',
            [String]$Filter = "*",
            [Int]$ToDepth = 1,
            [Int]$CurrentDepth = 0
        )
        #incrimintation
        $CurrentDepth++
    
        Get-ChildItem $Path | %{
            $_ | ?{ $_.Name -Like $Filter }
            If ($_.PsIsContainer) {
                If ($CurrentDepth -le $ToDepth) {
     
                    # Call to function
                    #adds the filter values and depth to the path..
                    Get-Depth -Path $_.FullName -Filter $Filter `
                      -ToDepth $ToDepth -CurrentDepth $CurrentDepth
                }
            }
        }
       
    }
    Get-Depth|? {$_.PsIsContainer}| select @{Name='Date Modified'; Expression={$_.LastWriteTime.ToString('MM/dd/yyyy')}}, @{Name='Owner'; E={(($_.GetAccessControl().Owner.Split('\'))[1])}}, @{Name='Structure'; E={(($_.FullName.Split('\'))[-1])}}
    

    Thank you!

  • #73949

    Participant
    Points: 0
    Rank: Member

    A couple of passes through split-path should do it – the drive and the filename will be stripped off just leaving the folder path

    PS> Get-ChildItem  .\HyperV\ -Recurse  | foreach {Split-Path -Path $_.Fullname  -Parent | Split-Path  -NoQualifier  }
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    

    BTW It is extremely bad practice to use aliases in your scripts ESPECIALLY ? and %

    • #73952

      Participant
      Points: 0
      Rank: Member

      Hi Richard,

      Thank you for the feedback. Can you suggest any alternatives for the aliases ?

      Thanks

    • #73957

      Participant
      Points: 2
      Rank: Member

      The recommendation for aliasses is: It's fine for the console for "quick 'n dirty" things, but do not use them in scripts. Never. 😉
      You can get a list of all alieasses with this:

      get-alias
    • #73964

      Participant
      Points: 0
      Rank: Member

      @Olaf Soyak Thank you! I will make those changes immediately, btw, i was using them in scripts... sorry.

The topic ‘split output fullname’ is closed to new replies.