Shortcut that will launch & run Office 365 PowerShell command line commands

I am looking for a way where I can click on a single file icon that will launch the Windows Power Shell and run the following commands:

$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
Connect-MsolService

So I will be prompted to and will be able to logon to Office 365 with my administrator account.

What is the best way to do this?
IT GuyNetwork EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ITSysTechSenior Systems AdministratorCommented:
You can do this by creating a file like Office 365.ps1 (create a .txt file and rename it) and put in the script, then to run it you right click the file and chose "Run with Powershell".
0
oBdACommented:
Save this is Office365.cmd or Whatever.cmd
It's PowerShell at heart, but wrapped in batch for easier execution.
You can add more Powershell commands that you want to automatically run below the first two lines.
@PowerShell.exe -NoExit -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 2) -join [environment]::NewLine)"
@exit /b %Errorlevel%
$UserCredential = Get-Credential -Message "Please enter the Office 365 credentials"
"Hello, $($UserCredential.UserName)" | Write-Host -ForegroundColor Yellow
Connect-MsolService -Credential $UserCredential

Open in new window

0
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
You can accomplish this a few ways. Here's two of them.

1. Save your script to a file (.ps1) and then create a .bat file that will launch powershell and run the file. e.g.:
powershell.exe -noexit -executionpolicy bypass -file C:\Office365.ps1

Open in new window


2. Convert your script to an encoded command and run that from the batch file. e.g.
 a. Encode your script:
$command = '@
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Connect-MsolService -Credential $LiveCred
@'

$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
$encodedCommand

Open in new window

Then take the output string and put it in the batch file:
powershell.exe -noexit -encodedCommand QAANAAoAJABMAGkAdgBlAEMAcgBlAGQAIAA9ACAARwBlAHQALQBDAHIAZQBkAGUAbgB0AGkAYQBsAA0ACgAkAFMAZQBzAHMAaQBvAG4AIAA9ACAATgBlAHcALQBQAFMAUwBlAHMAcwBpAG8AbgAgAC0AQwBvAG4AZgBpAGcAdQByAGEAdABpAG8AbgBOAGEAbQBlACAATQBpAGMAcgBvAHMAbwBmAHQALgBFAHgAYwBoAGEAbgBnAGUAIAAtAEMAbwBuAG4AZQBjAHQAaQBvAG4AVQByAGkAIABoAHQAdABwAHMAOgAvAC8AcABzAC4AbwB1AHQAbABvAG8AawAuAGMAbwBtAC8AcABvAHcAZQByAHMAaABlAGwAbAAvACAALQBDAHIAZQBkAGUAbgB0AGkAYQBsACAAJABMAGkAdgBlAEMAcgBlAGQAIAAtAEEAdQB0AGgAZQBuAHQAaQBjAGEAdABpAG8AbgAgAEIAYQBzAGkAYwAgAC0AQQBsAGwAbwB3AFIAZQBkAGkAcgBlAGMAdABpAG8AbgANAAoASQBtAHAAbwByAHQALQBQAFMAUwBlAHMAcwBpAG8AbgAgACQAUwBlAHMAcwBpAG8AbgANAAoAQwBvAG4AbgBlAGMAdAAtAE0AcwBvAGwAUwBlAHIAdgBpAGMAZQAgAC0AQwByAGUAZABlAG4AdABpAGEAbAAgACQATABpAHYAZQBDAHIAZQBkAA0ACgBAAA==

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.