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

  • Home
  • SEARCH
  • 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

We all know that you can overload a function according to the parameters: int
Can I overload a function which takes either a reference or variable name? For
I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
can any one explain this? Overload resolution and partial ordering are used to select
In a derived class If I redefine/overload a function name from a Base class,
I can't figure out how to define the default constructor (when it exists overloads)
I see in MSDN links such as CompareOrdinal Overloads. How can I write such
Can somebody point me to a resource that explains how to go about having

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.