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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:08:26+00:00 2026-06-18T08:08:26+00:00

I’m looking at writing some PowerShell code that can either execute immediately, or produce

  • 0

I’m looking at writing some PowerShell code that can either execute immediately, or produce the commands it would execute as generated scripts.

I’d like to avoid this scenario:

if($Generating){
  write-Output "somecommand.exe"
} 
else{
  somecommand.exe  
}

I got looking at ScriptBlocks, which at first looked promising because I can write the contents of the ScriptBlock to the console without executing it. Such as:

$sc = { somecommand.exe }
$sc
 somecommand.exe

My specific question is, if my scriptblock contains parameters, can I get them to resolve when I’m writing the scriptblock contents to the console, but WITHOUT invoking the scriptblock?

For example given the following scriptblock:

$b2 = { Param([string]$P) Write-Host "$P" }

When I just type “$b2” at the console and hit enter I see this:

Param([string]$P) Write-Host "$P"

What I’d like to see is this (if the parameter value is “Foo”):

Param([string]$P) Write-Host "Foo"

I realize this can be done when it’s invoked, either via “&” or using Invoke(), but would there be any way to get the parameters to resolve without invoking to make my script generation a little more elegant without needing a bunch of conditional statements throughout the code?

  • 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-18T08:08:27+00:00Added an answer on June 18, 2026 at 8:08 am

    Using some of the suggestions I think I’ve found the best solution for my needs. Consider the following code

    function TestFunc
    {
        Param(
            [Parameter(Mandatory=$true)]
            [string]$Folder,
    
            [Parameter(Mandatory=$true)]
            [string]$Foo
        )    
    
        $code = @"
    Write-Host "This is a folder $Folder"
    Write-Host "This is the value of Foo $Foo"
    "@
    
        $block = [Scriptblock]::Create($code)
    
        Write-Host "Running the block"  -BackgroundColor Green -ForegroundColor Black
        &$block
    
        Write-Host "Displaying block code" -BackgroundColor Green -ForegroundColor Black
        $block
    
    }
    

    And it’s output:

    Running the block
    This is a folder c:\some\folder
    This is the value of Foo FOOFOO
    Displaying block code
    Write-Host "This is a folder c:\some\folder"
    Write-Host "This is the value of Foo FOOFOO"
    

    By doing it this way, I still get all the benefit of keeping my existing functions and their parameters, parameter validation, CBH etc. I can also easily generate the code that the function would execute or just let it execute. Thanks for all the input, it’s definitely been a good learning experience.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.