Run multiple Powershell commands after each other

gnoble1979
gnoble1979 used Ask the Experts™
on
Hi

I want toGet-mailbox -identity "User1" | Export-Mailbox -IncludeFolders "\Sent Items" -SubjectKeywords "Joke666" -StartDate "05/11/2011" -RecipientKeywords john.doe@domain.com -pathtoPST c:\user1.pst

Get-mailbox -identity "User2" | Export-Mailbox -IncludeFolders "\Sent Items" -SubjectKeywords "Joke666" -StartDate "05/11/2011" -RecipientKeywords john.doe@domain.com -pathtoPST c:\user2.pst

And so on up until User80. Rather than me sitting at the keyboard doing one, waiting for it to complete, then another, is there a way to compile these so I can place them in an Excel file (for example) and just run them as one batch in Powershell?

Secondly, instead of the above, is there way a to run the following:

Get-content c:\userlist | Get-mailbox | Export-Mailbox -IncludeFolders "\Sent Items" -SubjectKeywords "Joke666" -StartDate "05/11/2011" -RecipientKeywords john.doe@domain.com -pathtoPST c:\<username>.pst

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Of course you can do it, you are almost there:

Get-content c:\userlist | Get-mailbox | foreach-object {Export-Mailbox -identity $_ -IncludeFolders "\Sent Items" -SubjectKeywords "Joke666" -StartDate "05/11/2011" -RecipientKeywords john.doe@domain.com -pathtoPST "c:\$($_.name).pst"}

Author

Commented:
That's great.

For my own knowledge, could you explain the above for me? What I am most confused about is the $ symbol and what it actually means!
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
$ always starts a variable name. $_ has a special meaning of "current object", when used in foreach-object. foreach-object performs the action (script) following it for each object given - which is the mailbox object. $_.name is the users name, hence.

$($_...) is a very special means in PS to concatenate strings with variable contents. It looks strange, but does nothing else then fill in the username into the path string.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Maybe
   -pathtoPST "c:\" + $_.name + ".pst"
is easier to understand for you.

Commented:
Let's say you have a string variable:

$a = "apple"

Open in new window


You can include the value of that variable in longer texts without contatenation with the quotationmarks:

"This is the value of my variable: $a"

Open in new window


This will return:
This is the value of my variable: apple

You can also get properties in quotationmarks:
"This is the length of my string in my variable: $($a.length)"

Open in new window


This will return:
This is the length of my string in my variable: 5

Without the extra $() you would get:
This is the length of my string in my variable: apple.length

If you want to use the $ sign literally, you can escape it by the 'backtick' character:

"This is the length of my `$a variable: $($a.length)"

Open in new window


This will return:
This is the length of my $a variable: 5
Chris DentPowerShell Developer
Top Expert 2010
Commented:
> $($_...) is a very special means in PS to concatenate strings with variable contents.

Not quite, it's a Sub-Expression. It means evaluate the contents of the inner-expression before doing stuff with whatever surrounds it.

We see it a lot with concatenation because when you do this:

"Hello $($_.Name)"

You need some way of telling PS that it must figure out what $_.Name is before it includes it in the string. Without that you'll end up with pretty odd output as soostibi has described.

Chris

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial