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 3237200
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:42:40+00:00 2026-05-17T17:42:40+00:00

Can you overload functions in PowerShell? I want to my function to accept a

  • 0

Can you overload functions in PowerShell?

I want to my function to accept a string, array or some switch.

An example of what I want:

  • Backup-UsersData singleUser
  • Backup-UsersData @(‘Alice’, ‘Bob’,
    ‘Joe’)
  • Backup-UsersData -all
  • 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-05-17T17:42:40+00:00Added an answer on May 17, 2026 at 5:42 pm

    In PowerShell functions are not overloaded. The last definition overrides the previous in the same scope or hides the previous in a parent scope. Thus, you should create a single function and provide a way to distinguish its call mode by arguments.

    In V2 you may use an advanced function, see help about_Functions_Advanced_Parameters and avoid some manual coding on resolving parameter set ambiguities:

    # advanced function with 3 parameter sets
    function Backup-UsersData
    (
        [Parameter(Position=0, ParameterSetName="user")]
        [string]$user,
        [Parameter(Position=0, ParameterSetName="array")]
        [object[]]$array,
        [Parameter(Position=0, ParameterSetName="all")]
        [switch]$all
    )
    {
        # use this to get the parameter set name
        $PSCmdlet.ParameterSetName
    }
    
    # test
    Backup-UsersData -user 'John'
    Backup-UsersData 1, 2
    Backup-UsersData -all
    
    # OUTPUT:
    # user
    # array
    # all
    

    Note that this mechanism is sometimes strange. For example in the first test we have to specify parameter name -user explicitly. Otherwise:

    Backup-UsersData : Parameter set cannot be resolved using the specified named parameters.
    At C:\TEMP\_101015_110059\try2.ps1:21 char:17
    + Backup-UsersData <<<<  'John'
        + CategoryInfo          : InvalidArgument: (:) [Backup-UsersData], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : AmbiguousParameterSet,Backup-UsersData
    

    In many cases standard, not advanced, function with mixed parameters will do:

    function Backup-UsersData
    (
        [string]$user,
        [object[]]$array,
        [switch]$all
    )
    {
        if ($user) {'user'}
        elseif ($array) {'array'}
        elseif ($all) {'all'}
        else {'may be'}
    }
    
    Backup-UsersData -user 'John'
    Backup-UsersData -array 1, 2
    Backup-UsersData -all
    Backup-UsersData
    

    But in this case you should resolve (or accept and ignore) ambiguities, e.g. to decide what to do if, say:

    Backup-UsersData -user 'John' -array 1, 2 -all
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to overload a function so that it manipulates its argument in some
Coming from C++ background ;) How can I overload PHP functions? One function definition
Possible Duplicate: «F(5)» and «int x; F(x)» to call different functions? Can we overload
We all know that you can overload a function according to the parameters: int
I recently discovered that in C++ you can overload the function call operator, in
Can't define (?) operator overload on type: type Foo = val s : string
Can I overload a function which takes either a reference or variable name? For
Consider these two functions: 'Overload 1 <Extension()> Function ComputeReturnValue(Of TSource, TResult)(ByVal source As TSource,
Related: Ambiguous overload accessing argument-less template functions with variadic parameters Simple variadic template function
In C++ is there a maximum number of functions that can overload each other?

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.