Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7708383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:35:55+00:00 2026-06-01T00:35:55+00:00

I’m trying to write a function that takes multiple arguments, which can come either

  • 0

I’m trying to write a function that takes multiple arguments, which can come either from the command line, or from the pipeline. The arguments can be strings or directory objects. The idea is that any of the following invocations should work:

Test-VEnv '.\MyPath', '.\AnotherPath'
Test-VEnv (dir)
'MyPath', 'AnotherPath' | Test-VEnv
dir | Test-VEnv

The following code almost works:

function Test-VEnv {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true, Position=0,
            ValueFromPipeline=$True,
            ValueFromPipelineByPropertyName=$true)]
        [Alias('FullName')]
        [String[]]$Path
    )

    process {
        foreach ($P in $Path) {
            ...
        }
    }
}

It handles strings both from the pipeline and the command argument, and handles directory objects from the pipeline (via ValueFromPipelineByPropertyName and the FullName alias). But it doesn’t handle directory objects on the command line, so

dir | Where-Object { Test-VEnv $_ }

fails, as it converts the directory objects to strings, which uses the Name property rather than FullName, and the subsequent code fails.

Can anyone tell me how to achieve what I want?

I am aware that even if I can get this to work, it may not be a particularly good design. But as far as I can tell, it’s how the built in Test-Path works, so I want to try following standard behaviour before I invent my own…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:35:56+00:00Added an answer on June 1, 2026 at 12:35 am

    Since your parameter type is string it’s coercing the file system info object into a string when you are not using the pipeline { Test-VEnv $_ }. If you call the ToString() method of either a System.IO.FileInfo or System.IO.DirectoryInfo object you’ll see this. When you use the pipeline it binds the fullname alias giving you the full path.

    You can see what PowerShell is doing to bind the input object using Trace-Command. Here is an example of how to use it:

    trace-command -name parameterbinding -expression {(dir C:\)[0] | ? {Test-VEnv $_}} -pshost
    

    Here is the important part of the output:

    BIND arg [PerfLogs] to parameter [Path]
        Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute]
            result returned from DATA GENERATION: System.String[]
        COERCE arg to [System.String[]]
            Parameter and arg types the same, no coercion is needed.
        BIND arg [System.String[]] to param [Path] SUCCESSFUL
    

    Test-Path does the same thing. Take a look at these three examples:

    PS C:\Users\Andy> Test-Path (dir C:\)[0]
    False
    PS C:\Users\Andy> (dir C:\)[0] | Test-Path
    True
    PS C:\> Test-Path (dir C:\)[0]
    True
    
    1. Since my PWD is not C:\ I get FALSE because the DirectoryInfo object is converted to string (ToString()) which only gives the folder name. This is because the pipeline wasn’t used.

    2. Since the pipeline is used it works because it is binding to PsPath with this parameter:

      [Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
      [Alias('PSPath')]
      [string[]]
      ${LiteralPath},
      
    3. Since the directory contains the folder the folder’s name exists.

    You might try the alias PsPath for your binding. This is what Test-Path uses:

    param (
        [Parameter(Mandatory=$true, Position=0,
            ValueFromPipeline=$True,
            ValueFromPipelineByPropertyName=$true)]
        [Alias('PsPath')]
        [String[]] $Path
    )
    
    process {
        foreach ($P in $Path) {
            Get-Item $p
        }
    }
    

    Some tests:

    Set-Location C:\
    Write-Host 1
        Test-VEnv '.\Windows', '.\Program Files'
    Write-Host 2
        Test-VEnv (dir)
    Write-Host 3
        'Windows', 'Program Files' | Test-VEnv
    Write-Host 4
        dir | Test-VEnv
    

    Output:

    1
        Directory: C:\
    Mode                LastWriteTime     Length Name                                                       
    ----                -------------     ------ ----                                                       
    d----         3/14/2012   3:41 AM            Windows                                                    
    d-r--         3/24/2012   7:46 PM            Program Files                                              
    
    2
    d----         2/18/2012   4:32 AM            PerfLogs                                                   
    d-r--         3/24/2012   7:46 PM            Program Files                                              
    d-r--         3/25/2012   4:49 PM            Program Files (x86)                                        
    d----          3/9/2012   9:57 PM            Python27                                                   
    d-r--          3/4/2012   8:11 PM            Users                                                      
    d----         3/14/2012   3:41 AM            Windows                                                    
    -a---          3/4/2012   8:45 PM       1024 .rnd                                                       
    
    3
    d----         3/14/2012   3:41 AM            Windows                                                    
    d-r--         3/24/2012   7:46 PM            Program Files                                              
    
    4
    d----         2/18/2012   4:32 AM            PerfLogs                                                   
    d-r--         3/24/2012   7:46 PM            Program Files                                              
    d-r--         3/25/2012   4:49 PM            Program Files (x86)                                        
    d----          3/9/2012   9:57 PM            Python27                                                   
    d-r--          3/4/2012   8:11 PM            Users                                                      
    d----         3/14/2012   3:41 AM            Windows                                                    
    -a---          3/4/2012   8:45 PM       1024 .rnd  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.