Skip to content

Expand the user guide with info about escaping in PowerShell #3638

@martinlingstuyl

Description

@martinlingstuyl

The CLI documentation could use some information about escaping in PowerShell.

Escaping double quotes

There's an issue in PowerShell 5 and 7 (a fix is implemented for POSH 7.3, but that's not live yet.) around parsing command arguments:
PowerShell/PowerShell#1995

The idea is that command arguments are being parsed twice for tools like the CLI. Once by PowerShell and once by the executable that's being called. The result is that you need to escape quotes twice.

Example
The following code

m365 spo listitem set --webUrl "https://contoso.sharepoint.com/sites/Project-x" --id 1 --listTitle somelist --SomeField "{ `"test1`": `"test2`" }"

Would result in the following being saved to sharepoint: { test1: test2 }. As you can see: the double quotes are missing.

There are two methods to work around this:

Method 1: Escaping twice:
In this situation you escape the double quotes for powershell, using a backtick (`) AND you escape it for the executable using backslash ():

m365 spo listitem set --webUrl "https://contoso.sharepoint.com/sites/Project-x" --id 1 --listTitle somelist --SomeField "{ \`"test1\`": \`"test2\`" }"

Method 2: Using verbatim strings with single quotes:
In this situation you surround your value with a single quote, meaning that you don't have to escape double quotes in powershell. So no backtick. You still need to use a backslash though, to take care of the parsing in the lower level executable:

m365 spo listitem set --webUrl "https://contoso.sharepoint.com/sites/Project-x" --id 1 --listTitle somelist --SomeField '{ \"test1\": \"test2\" }'

Escaping @ and $

Some extra information about escaping @ when using tokens and $, for example in URL's, would be handy as well.

Where to add the explanations

We could add the explanation below the existing section here: https://pnp.github.io/cli-microsoft365/user-guide/using-cli/#values-with-quotes

But also: I think we should add pointers to this central documentation at least in every spot where this is evidently used, like with sending adaptive cards and setting taxonomy. All places where double quotes or @ tokens or $ are included in examples.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions