Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PowerShell for Office 365

You're reading from   PowerShell for Office 365 Automate Office 365 administrative tasks

Arrow left icon
Product type Paperback
Published in Jul 2017
Publisher Packt
ISBN-13 9781787127999
Length 222 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Prashant G Bhoyar Prashant G Bhoyar
Author Profile Icon Prashant G Bhoyar
Prashant G Bhoyar
Martin Machado Martin Machado
Author Profile Icon Martin Machado
Martin Machado
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. PowerShell Fundamentals FREE CHAPTER 2. Managing Office 365 with PowerShell 3. Azure AD and Licensing Management 4. Managing SharePoint Online Using PowerShell 5. Managing Exchange Online Using PowerShell 6. Script Automation 7. Patterns and Practices PowerShell 8. OneDrive for Business 9. PowerShell Core

How to pass parameters to cmdlets and storing results as a variable

A cmdlet is a lightweight command that is used in the Windows PowerShell environment. The Windows PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line. The Windows PowerShell runtime also invokes them programmatically through Windows PowerShell APIs.

They basically accept input via parameters, perform the operation, and then output the results.

Cmdlets differ from commands in a command-shell environment in the following ways:

  • Cmdlets are instances of .NET Framework classes; they are not standalone executables.
  • Cmdlets can be created from as few as a dozen lines of code.
  • Cmdlets do not generally do their own parsing, error presentation, or output formatting and these operations are normally handled by the Windows PowerShell runtime.
  • Cmdlets process input objects from the pipeline rather than from streams of text, and typically deliver objects as output to the pipeline.
  • Cmdlets are record-oriented because they process a single object at a time.

Parameters

Parameters are the input values that we pass to a cmdlet. For example, if we have to get the time zone, we can use the following cmdlet:

Get-TimeZone

This cmdlet gets the current time zone or a list of available time zones.

These are the parameters:

  • [-Id]: Specifies, as a string array, the ID or IDs of the time zones that this cmdlet gets
  • [-ListAvailable]: Indicates that this cmdlet gets all available time zones
  • [-Name]: Specifies, as a string array, the name or names of the time zones that this cmdlet gets

We can use this command with or without these parameters:

Get-TimeZone

The following screenshot shows the output for the preceding command:

We can use this command with the Name parameter:

Get-TimeZone -Name "*pac*"

The following screenshot shows the output for the preceding command:

We can use this command with the ListAvailable parameter:

Get-TimeZone -ListAvailable

The following screenshot shows the output for the preceding command:

In PowerShell, variables are always prefixed by the character $ and can include any alphanumeric character or underscore in their names. We can store the output from a cmdlet in a variable and use it later on in other cmdlets or for other purposes in the script, such as writing to the host, using it for comparison, or creating another variable, such as this, for example:

$timeZone = Get-TimeZone
Write-Host "The current time zone is " $timeZone

The following screenshot shows the output for the preceding command:

You have been reading a chapter from
PowerShell for Office 365
Published in: Jul 2017
Publisher: Packt
ISBN-13: 9781787127999
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime