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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:22:34+00:00 2026-05-18T08:22:34+00:00

By default, any named function that has the [CmdletBinding()] attribute accepts the -debug and

  • 0

By default, any named function that has the [CmdletBinding()] attribute accepts the -debug and -verbose (and a few others) parameters and has the predefined $debug and $verbose variables. I’m trying to figure out how to pass them on to other cmdlet’s that get called within the function.

Let’s say I have a cmdlet like this:

function DoStuff() {
   [CmdletBinding()]

   PROCESS {
      new-item Test -type Directory
   }
}

If -debug or -verbose was passed into my function, I want to pass that flag into the new-item cmdlet. What’s the right pattern for doing this?

  • 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-18T08:22:35+00:00Added an answer on May 18, 2026 at 8:22 am

    Perhaps it sounds strange, but there isn’t any easy way for a cmdlet to know its verbose or debug mode. Take a look at the related question:

    How does a cmdlet know when it really should call WriteVerbose()?

    One not perfect, but practically reasonable, option is to introduce your own cmdlet parameters (for example, $MyVerbose and $MyDebug) and use them in the code explicitly:

    function DoStuff {
        [CmdletBinding()]
        param
        (
            # Unfortunately, we cannot use Verbose name with CmdletBinding
            [switch]$MyVerbose
        )
    
        process {
    
            if ($MyVerbose) {
                # Do verbose stuff
            }
    
            # Pass $MyVerbose in the cmdlet explicitly
            New-Item Test -Type Directory -Verbose:$MyVerbose
        }
    }
    
    DoStuff -MyVerbose
    

    UPDATE

    When we need only a switch (not, say, a verbosity level value) then the approach with $PSBoundParameters is perhaps better than proposed in the first part of this answer (with extra parameters):

    function DoStuff {
        [CmdletBinding()]
        param()
    
        process {
            if ($PSBoundParameters['Verbose']) {
                # Do verbose stuff
            }
    
            New-Item Test -Type Directory -Verbose:($PSBoundParameters['Verbose'] -eq $true)
        }
    }
    
    DoStuff -Verbose
    

    It’s all not perfect anyway. If there are better solutions then I would really like to know them myself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that has an optional variable myFont . By default, it's
I am using the Zend Framework. I have a controller named 'UserController' that has
Is there any default functionality for arranging JInternalFrames in Java Swing? I would like
Do PHP sessions timeout by default - ie without any coding on my part
Does any one have tried to customize default section index displayed in UITableView. I
Is there any way to have a default MessageBox.Show() caption? Let says I would
Is there any sense to declare default constructor in Java? class MyClass { public
For any given type i want to know its default value. In C#, there
Is there any way to change default font of controls in winforms. I must
Is there any way to suppress the default js and css loaded by application.html.erb

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.