PowerShell is a cross-platform tool, that is able to perform many administrative operations and changes in the OS (to be clear not only for OS). PowerShell is not only a tool, it is a solution for administrators to perform scripts, configure many things across f.e. domain or even whole tenant of your company. Bulk operations were never easier thanks to that solution. Managing users, creating and configuring mailboxes or even create Conditional Access Policies.
A brief history of PowerShell
PowerShell was first introduced in 2006 for Windows XP SP2, Windows 2003 SP1, and Windows Vista while remaining as an optional feature for Windows Server 2008. Below the first version of PowerShell.
As you can see it really not differs much from the present version presented below:
Despite difference (on the first glance) in the visual aspect, which is related mainly to the running OS and it’s settings, the magic hides behind in the core of the Shell. Since the first release of PowerShell there were many features added such as script debugging, block comments (useful for script writers), eventing and scheduling jobs for Windows Task Scheduler.
A short list of the pros and cons of using PowerShell
Below is a short list of pros and cons of using the PowerShell to manage mailboxes, Exchange properties and other Microsoft Services manageable by PowerShell and etc.
- Much more knowledge about what is done to specified object (Azure Object, Mailbox).
- Object management easily modified (mostly) using specified commands.
- Much more info regarding specified object (many properties not seen in GUI).
- Bulk operations using *.csv files (for example a list of users or other objects to be modified using the same attribute).
- You can use variables just like any programming language.
- Some of managed attributes require other modules to be installed (not a con, but you still need to install the module).
- You need to know what you are doing! – most of the commands are easy to use and you can predict what will happen, but some of them can cause disaster to your tenant.
Example of PowerShell usage for the administrators with a little backstory of our app
During my first weeks in the company I learned a lot about Azure and its capabilities. While I was lurking in the processes and the functions I reckon that some of the tasks can be performed using simple PowerShell commands instead of *click* *click* via GUI (Graphic User Interface). With my colleague we came up with an idea. How about creating our own “app” for some of the operations? Since the idea and start of the realization there were about 20 minutes. I started gathering info about needed modules, what is needed to be installed and how to make this all real thing.
Implementation of the idea in a few steps
All of the next operations are needed to be run as administrator.
- First thing to do is to get the needed modules, but to get them we need to have proper package providers.
To start we need to install the “NuGet” using: Install-PackageProvider -Name NuGet -Force:
- When it is installed we proceed to find the exact module that is needed to be installed in order to get our work done. For our app we needed something that will do some tasks regarding Exchange Online. To find a specified module we needed to look for it in repositories. The easiest way is to find it’s name using Find-Module command.
- The next step is the installation of desired module. In our case it is ExchangeOnlineManagement. To install simply use the command: Install-Module -Name ExchangeOnlineManagement -Repository *YourRepositoryName*
You need to specify from which repository you are downloading the module otherwise you will receive an error.
- After the module is installed to make sure, you can run the Get-InstalledModule to list all of the modules installed for Your PowerShell, where You should see now the module name installed. To check you can also try running ‚Connect-ExchangeOnline’ which after triggering should prompt you for your creditentials to log in to your tenant Exchange.
- Successful login will simply line up for another PS commands to be written. From now on you will be able to do (almost) whatever you want inside tenant Exchange Online server.
A disclaimer before proceeding
Many of the commands you can use have an additional modifier for the command which is ‚-WhatIf’. When used at the end of the command you will see an output for the command and show what will be outcome of performing such a command. On example below you can see the proper work of the modifier.
Creation of one application
After installing everything I was ready to work further. Simple scripts were not enough for me as I still had to copy paste everything and run the scripts again. Me and my colleague came up to an idea to summarize everything into one single smooth app, which would allow us to go do the repetitive tasks even faster.
The info regarding ‚our GUI’ was google and other sites where we gathered info how to create it, implement an icon, text on top of the app window and etc. Then all we had to do was to implement commands into the task boxes and test everything. Below you can see the final result of the work.
In the picture you can see the possibilities of our app. It can be edited to work under your own tenant. The working principle is the text box in which you paste the URL (without http:// or https://) and then click on ‚Block URL’. It will perform a simple command assigned under the button. When field is empty a popup window comes up (info for us that the field cannot be empty) and it will disconnect the session, so we will have to log in again (just for security purposes). The correct performance of command will also be shown as popup but with information that task has been completed successfully.
Below there is a little section from the whole script where the button comes to live by defining its parameters and the command itself to be performed.
Conclusion at the end
As you can see this is not a rocket science and a simple working app can be done in PowerShell to improve the work of an administrator for prosaic cases like blocking an URL, domain and even purging spam/phish mails from users mailboxes.
The app can be edited to do more than that! By adding a few text boxes you can improve its functionality by for example adding a search query for content search from Security&Compliance feature from Microsoft. Then you could do all of the tasks regarding spam/phish tasks.
This is just a little example of what PowerShell is capable of and administrators who are more familiar with it could do more difficult and complex cases by simply running a few lines of code, when doing it by GUI would take up to few hours of ‚clicking’. In addition to that Azure has its own CLI in which you can run PowerShell commands so this little “helping hand” available for many years is not an exclusive for On-Premises environment.