1

I'm trying the following:

 $Result = Invoke-RestMethod "https://graph.microsoft.com/v1.0/groups?$top=1" 

But the ?$top=1 part just doesn't work. I know it should, because when testing in Microsoft Graph explorer, it does!..

So WTH am I doing wrong?!

2
  • 1
    Did you try to use single quotes? $Result = Invoke-RestMethod 'https://graph.microsoft.com/v1.0/groups?$top=1', see help about_quoting_rules Commented Jul 29, 2020 at 13:41
  • That worked. Crazy, and absolutely ridiculous. I can only imagine how many people have buggy scripts and a headache because of this. Either enforce single quotes, or give me the same result. Commented Jul 29, 2020 at 13:43

2 Answers 2

1

You have to use single quotes instead of double quotes:

$Result = Invoke-RestMethod 'https://graph.microsoft.com/v1.0/groups?$top=1'

The reason is that with dobule qoutes Powershell tries to evaluate the $top expression, see: help about_quoting_rules

Sign up to request clarification or add additional context in comments.

Comments

0

In Powershell, if a string is enclosed in double-quotes then expressions and special characters inside will be evaluated and expanded. The $ sign in your $top expression is being evaluated and the value of the $top variable is being inserted in there.

This is super useful for a lot of reasons, but if you don't want it to happen, then single quotes will work (as others have mentioned). Or you can escape the dollar sign using the backtick operator. E.g.

$Result = Invoke-RestMethod "https://graph.microsoft.com/v1.0/groups?`$top=1"

Read how quoting rules work here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.