1

How do I combine a variable with the rest of the command, without getting an error:

i.e. I want to issue:

Set-MailboxFolderPermission -Identity [email protected]:\Calendar -User Default -AccessRights Reviewer

But using a variable $email for the email address. I tried:

Set-MailboxFolderPermission -Identity $email:\Calendar -User Default -AccessRights Reviewer

But this gives an error.

3 Answers 3

2

You can also use braces to isolate the variable. Notice that with this syntax the variable name doesn't include the dollar sign:

"${email}:\Calendar"
Sign up to request clarification or add additional context in comments.

2 Comments

No joy. + CategoryInfo : ObjectNotFound: (email:String) [], CommandNotFoundExceptionCannot bind argument to parameter 'Identity' because it is null.
Does it help if you wrap it in double quotes? (e.g "${email}:\Calendar")
2

The colon (:) is a valid character in variable names (it's a scope specifier or a PSDrive reference) and thus it looks for the variable $email: (which in itself is invalid, but still problematic here). You need either a subexpression and quotes:

"$($email):\Calendar"

or escape the colon:

$email`:\Calendar

5 Comments

Thanks but both of these are giving me 'The specified mailbox "[email protected]" doesn't exist.' It does.
The error is then somewhere else, your variable embedding/quoting problem is solved, however. Look further, then.
@Joey - just realized. You "ate" \ in your answer... :) Probably that's the reason for the error messages.
Aaah, that could be, indeed. Thanks. It might have disappeared when I changed the inline code to block code because Markdown doesn't really like ` as an escape character. Shay has the better answer, though; I tend to forget about ${...} mostly because I rarely need it when golfing.
I did put the \ in even though you ate it. Didn't work anyway. Thx.
0

The variable wont expand unless its in double quotes:

set-MailboxFolderPermission -Identity "$email:\Calendar" -User Default -AccessRights Reviewer

2 Comments

Thanks but I just tried your suggestion and got 'The specified mailbox "\Calendar" doesn't exist.'
finisterre: That's because $email: expands to nothing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.