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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:49:22+00:00 2026-06-12T20:49:22+00:00

I was hoping that I could setup a cmdlet so that it would use

  • 0

I was hoping that I could setup a cmdlet so that it would use an environment variable for a parameter value if it exists, or otherwise prompt.

function Test-Mandatory
{
  [CmdletBinding()]
  param(
    [Parameter(Mandatory = { [string]::IsNullOrEmpty($Env:TEST_PARAM) })]
    [string]
    $foo = $Env:TEST_PARAM
  )

  Write-Host $foo
}

Unfortunately, it seems that regardless of whether or not I have a $Env:TEST_PARAM set, the cmdlet always prompts for $foo.

I could rework the validation to use [ValidateScript({ #snip #}), but then I wouldn’t get Powershell prompting for the required value anymore, should $Env:TEST_PARAM not exist. I would simply get a validation error.

So 2 questions here

  • Why can I even assign a scriptblock to Mandatory if it doesn’t appear to be honored?
  • Is there a simple way to get the default PS prompting behavior given the criteria I specified?
  • 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-12T20:49:23+00:00Added an answer on June 12, 2026 at 8:49 pm

    FYI, here’s what Microsoft had to say about it:

    “It’s not really a bug. Script blocks are valid attribute arguments –
    e.g. ValidateScriptBlock wouldn’t work very well otherwise.
    Attribute arguments are always converted to the parameter type. In
    the case of Mandatory – it takes a bool, and any time you convert a
    ScriptBlock to bool, you’ll get the value $true. You never invoke a
    script block to do a conversion.”

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

Sidebar

Related Questions

I was hoping that I could pass a DateVersionSpec into VersionControlServer.DownloadFile() but it doesn't
I was just hoping that someone could point me to a reference that defines
Was hoping I could get a bit of a hand on this login that
Hoping that someone can help clear up this very annoying situation I find myself
Hoping that anybody here knows about a good one: I'm looking for a (free
Hoping that using something like this demo it is possible to drag items within
I'm hoping that i'm simply missing something, but I really can't see how to
I am hoping that this will have a pretty quick and simple answer. I
I am hoping that there might be an easy way to do this, I
I have this: And I was hoping that in f1() I will be able

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.