Avatar of gnoble1979
gnoble1979

asked on 

Run multiple Powershell commands after each other

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

Powershell

Avatar of undefined
Last Comment
Chris Dent
ASKER CERTIFIED SOLUTION
Avatar of soostibi
soostibi
Flag of Hungary image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gnoble1979
gnoble1979

ASKER

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!
Avatar of Qlemo
Qlemo
Flag of Germany image

$ 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.
Avatar of Qlemo
Qlemo
Flag of Germany image

Maybe
   -pathtoPST "c:\" + $_.name + ".pst"
is easier to understand for you.
Avatar of soostibi
soostibi
Flag of Hungary image

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
SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo