September 18, 2020

Samir Daoudi's Technical Blog

SharePoint, Office 365, C#, .Net, SQL Server News, Tips, Ebooks, Events, Webinars ….etc

PowerShell (Part 1) – Getting Started

In this post we will get started with PowerShell for Office 365, before digging into more complex details related to the administration fo SharePoint, Exchange Online, Office365 Users…etc, let’s first start by getting some basic knowledges about PowerShell.

1- Setting up your environment

Windows

For windows users, most of the Windows editions > Windows 7 SP1, will have PowerShell installed by default, if not, you can enable it  by following this simple article from Microsoft

https://docs.microsoft.com/en-us/powershell/scripting/getting-started/starting-windows-powershell?view=powershell-7

MacOS

My preferred Device and OS 😏 (Sorry MS, I tried all the surface devices, but can’t separate from my Mac 🤣)

  • To Install PowerShell,  you can simply open a terminal and run
brew cask install powershell

This assuming you have brew installed on your MacOS, if not, here is how you can install it : https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-macos?view=powershell-7#about-brew

  • To start PowerShell, just type the following command
pwsh
  • To check for an update, and install the latest version of PowerShell
brew update
brew cask upgrade powershell

2- Getting started with PowerShell

Be re-assured, you are not expected and might not be able to remember all the commands, properties, members …etc available in PowerShell (if you do, you need to get a life 😅).

However, you can easily get help from PowerShell, using some of the existant commands such as :

  • Get-Command

    • This command allows you to list all the commands available in PowerShell, you can filter by a sub string, for example, if I am looking for all the commands with the word Host, I can do:
      Get-Command *Host
  • Get-Help

    • This command allows you to get the details of a specific command, for example, I want to know what does the Get-PSDrive command do and how can I use it (which parameters does it expect and what results does it return), for this, I can simply type:
      Get-Help Get-SPDrive

      Which will return the following output

    • If you want to see the online help for this command, you can simply type:
      Get-Help Get-SPDrive -Online
  • Get-Member
    • This command allows you to list all the properties & methods for a specific command. For example, I want to see the members and properties of the Get-Service command, I can simply type:
    • Get-Service | Get-Member

       

3- Update the help

If the help you get from Get-Help is not up to date, or the Get-Help can’t find a specific command (this is very frequent when you install a new module), you can simply update the help section which will download all the files and details of the available commands, simply type:

Update-Help

I will skip the part where we talk about how to Write-Host, do a loop, declare a variable …etc, as this is just a quick introduction before we jump into the more serious things related to Office365.

I believe there are plenty of ressources online if you want to improve your PowerShell skills, but for me, I will just give you what we need to jump to the next step which is way more fun 😁.

4- Create a function

You can easily create a function and call from different places in your code.

Please be aware that similarly to JavaScript the function needs to be declared before you call it.

function Add ($a, $b)
{
    $result = $a+$b
    Write-Host "The result is $result"
}

5- Call a function

You can simply do:

add 3,4

6- Reference another .PS1 file

Let’s say you have your library of functions (Send Email, Write to Windows Log, Encrypt …etc.) you can reference your functions.PS1 file from another .PS1 script and call the functions listed in that file. For this, just type in your Main.ps1

. ./functions.ps1

What’s next ?

In the next post, I will take you through the modules you need to install to be able to authenticate and connect to your Office365 tenant and be able to administer it properly.

Leave a comment

Copyright © All rights reserved. | Newsphere by AF themes.
Show Buttons
Hide Buttons

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.