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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:25:21+00:00 2026-05-14T23:25:21+00:00

This is a very simple task in every language I have ever used, but

  • 0

This is a very simple task in every language I have ever used, but I can’t seem to figure it out in PowerShell. An example of what I’m talking about in C:

abs(x + y)

The expression x + y is evaluated, and the result passed to abs as the parameter… how do I do that in PowerShell? The only way I have figured out so far is to create a temporary variable to store the result of the expression, and pass that.

PowerShell seems to have very strange grammar and parsing rules that are constantly catching me by surprise, just like this situation. Does anyone know of documentation or a tutorial that explains the basic underlying theory of the language? I can’t believe these are all special cases, there must be some rhyme or reason that no tutorial I have yet read explains. And yes, I’ve read this question, and all of those tutorials are awful. I’ve pretty much been relegated to learning from existing 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-05-14T23:25:21+00:00Added an answer on May 14, 2026 at 11:25 pm

    In your case, simply surrounding the expression with parenthesis will allow you to pass it to your function.


    You need to do this because PowerShell has more than one parsing mode depending on the beginning of the command.

    Expression mode is similar to how most other languages parse – numbers are numbers and strings are quoted.

    Command mode treats everything as a string except for variables and parenthesis. Strings here don’t need to be quoted.

    1+2          Expression mode - starts with number
    "string"     Expression mode - starts with quote
    string       Command mode - starts with letter
    & "string"   Command mode - starts with &
    . "string"   Command mode - starts with . and a space
    .123         Expression mode - starts with . and number (without space)
    .string      Command mode - starts with a . that is part of a command name
    

    You can mix modes in a single line by enclosing the commands with parenthesis.

    You can see this effect if you define function abs in the following way:

    function Abs($value)
    {
        Write-Host $args
        if($value -lt 0) { -$value } else { $value }
    }
    
    Abs 1 + 2
    #Prints:  + 2
    #Returns: 1
    
    Abs 1+2
    #Prints:  
    #Returns: 1+2
    
    Abs (1 + 2)
    #Prints:  
    #Returns: 3
    
    Abs (1+2)
    #Prints:  
    #Returns: 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a homework task, but it's very simple. The task comes with a
I have the following issue whith this very simple task. I want to create
It's very simple task, but I struggle for hours. I have parse xml from
I have a very simple method scheduled to run every 10 seconds like this:
This should be a very simple task, but for some reason I'm running into
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have this very simple example that I am using to learn structs in
I was making this very simple lex program (just an introductory program). But on
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
I have this very simple C++ class: class Tree { public: Node *head; };

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.