{"id":4382,"date":"2020-04-13T23:55:05","date_gmt":"2020-04-14T06:55:05","guid":{"rendered":"https:\/\/officedevblogs.wpengine.com\/?p=4382"},"modified":"2020-04-13T23:55:05","modified_gmt":"2020-04-14T06:55:05","slug":"getting-started-office365-cli-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/getting-started-office365-cli-powershell\/","title":{"rendered":"Getting started with Office 365 CLI and PowerShell"},"content":{"rendered":"<p>In this post, we will show you how to get started with using the Office 365 CLI with the popular PowerShell scripting language on Windows and now also cross platform using PowerShell Core (<code class=\"language-plaintext highlighter-rouge\">pwsh<\/code>).<\/p>\n<h3 id=\"why-would-i-want-to-use-the-office-365-cli\">Why would I want to use the Office 365 CLI?<\/h3>\n<p>As more and more organizations move into Office 365, more solutions are being built that expand beyond the Windows operating system. Developers and administrators are no longer constrained to Microsoft development technologies tied to the Windows platform, opening up the possibility to develop applications and automation scripts using cross platform technologies to authenticate and communicate with Office 365 workloads.<\/p>\n<p>Unfortunately, managing many of the workload settings is possible only through using PowerShell on Windows. It is typically for developers and administrators to use a combination of different PowerShell modules to achieve this, such as <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/sharepoint\/sharepoint-pnp\/sharepoint-pnp-cmdlets?view=sharepoint-ps\">PnP PowerShell<\/a> and <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/sharepoint\/sharepoint-online\/introduction-sharepoint-online-management-shell?view=sharepoint-ps\">SharePoint Management Shell<\/a>. For non-Windows users to handle such a scenario, it is common for them to use Windows Virtual Machines just to manage these settings, which is not convenient and can be costly.<\/p>\n<p>The <a href=\"https:\/\/github.com\/pnp\/office365-cli\">Office 365 CLI<\/a> is a cross-platform command line interface that enables you to manage your Office 365 tenant on any operating system or shell, whether you are on Windows, MacOS, or Linux, or use CMD, PowerShell, bash or zsh.<\/p>\n<p>The CLI also provides a single unified login to Office 365 workloads. It is becoming more and more common that you need to manage and provision assets in many different workloads in the same script and it is inconvenient to connect\/disconnect with them individually. The CLI handles this complexity for you, enabling you to concentrate on your script logic without having to handle the authentication requirements for each workload as this is managed behind the scenes for you.<\/p>\n<h3 id=\"installing-the-office-365-cli\">Installing the Office 365 CLI<\/h3>\n<blockquote><p>The CLI runs on NodeJS, therefore it does require that you have Node v6 or higher installed on your machine.<\/p><\/blockquote>\n<p>Open up a PowerShell session and install the CLI using <code class=\"language-plaintext highlighter-rouge\">npm<\/code>, once installed, this will be available to use in your shell.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>PS &gt; npm install @pnp\/office365-cli -g\n<\/code><\/pre>\n<\/div>\n<\/div>\n<blockquote><p>Learn more about installing the Office 365 CLI <a href=\"https:\/\/pnp.github.io\/office365-cli\/user-guide\/installing-cli\/\">here<\/a><\/p><\/blockquote>\n<h3 id=\"setup-command-completion\">Setup command completion<\/h3>\n<p>One of the key features of PowerShell is that tabbed command completion is built in by default. If you want to find a command to use you can simply <code class=\"language-plaintext highlighter-rouge\">tab<\/code> and move through the different cmdlets available to you.<\/p>\n<p>With the 2.7 (March 2020) release of the Office 365 CLI, we added command completion functionality for PowerShell, to enable a similar experience when using the CLI.<\/p>\n<p>To enable command completion, we have provided a command in the CLI to help do this for you.<\/p>\n<p>Using the <code class=\"language-plaintext highlighter-rouge\">cli completion pwsh setup<\/code> command adds a reference in your PowerShell profile which loads the CLI command completion when you open a new PowerShell session.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>PS &gt; o365 cli completion pwsh setup --profile $PROFILE\n<\/code><\/pre>\n<\/div>\n<\/div>\n<blockquote><p>Notice that we prefix the <code class=\"language-plaintext highlighter-rouge\">cli completion pwsh setup<\/code> command with <code class=\"language-plaintext highlighter-rouge\">o365<\/code> in the above example. To execute any CLI command using PowerShell, we must prefix them with <code class=\"language-plaintext highlighter-rouge\">o365<\/code> or <code class=\"language-plaintext highlighter-rouge\">office365<\/code> to access the CLI in non-immersive mode.<\/p>\n<p>Learn more about immersive and non-immersive mode <a href=\"https:\/\/pnp.github.io\/office365-cli\/user-guide\/using-cli\/#start-the-cli\">here<\/a>.<\/p><\/blockquote>\n<p>You should close your current PowerShell session after running the above command for the changes to take effect.<\/p>\n<p>When you next use the CLI commands, you can use <code class=\"language-plaintext highlighter-rouge\">&lt;tab&gt;<\/code> to return options available to you, for example, <code class=\"language-plaintext highlighter-rouge\">o365 &lt;tab&gt;<\/code> to return all top level command groups.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-large wp-image-4383\" src=\"https:\/\/officedevblogs.wpengine.com\/wp-content\/uploads\/2020\/04\/pwsh_command_completion-1024x528.png\" alt=\"Office 365 CLI commands displayed in PowerShell Core\" width=\"1024\" height=\"528\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2020\/04\/pwsh_command_completion-1024x528.png 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2020\/04\/pwsh_command_completion-300x155.png 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2020\/04\/pwsh_command_completion-768x396.png 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2020\/04\/pwsh_command_completion.png 1440w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<blockquote><p>With every release of the Office 365 CLI, (beta releases are weekly and major releases are monthly), we add new commands for you to use.<\/p>\n<p>To ensure that you can use these new commands in command completion, you need to update, to do that use the <code class=\"language-plaintext highlighter-rouge\">update<\/code> command.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>PS &gt; o365 cli completion pwsh update\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/blockquote>\n<h4 id=\"view-command-help\">View command help<\/h4>\n<p>In PowerShell, you can the <code class=\"language-plaintext highlighter-rouge\">Get-Help<\/code> cmdlet to return interactive help for a given cmdlet as the Office 365 CLI is not built using PowerShell it is not possible to use this, however we do provide a <code class=\"language-plaintext highlighter-rouge\">--help<\/code> option on all commands in the CLI.<\/p>\n<p>Use this to return information on what the command does, the options available and examples of how to use the command.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>PS &gt; o365 spo web get --help\n\n  Usage: spo web get [options]\n\n  Retrieve information about the specified site\n\n  Options:\n\n    --help                 output usage information\n    -u, --webUrl &lt;webUrl&gt;  URL of the site for which to retrieve the information\n    --query [query]        JMESPath query string. See http:\/\/jmespath.org\/ for more information and examples\n    -o, --output [output]  Output type. json|text. Default text\n    --pretty               Prettifies json output\n    --verbose              Runs command with verbose logging\n    --debug                Runs command with debug logging\n\n  Examples:\n  \n    Retrieve information about the site https:\/\/contoso.sharepoint.com\/subsite\n      spo web get --webUrl https:\/\/contoso.sharepoint.com\/subsite\n<\/code><\/pre>\n<\/div>\n<\/div>\n<h3 id=\"authenticating-with-your-office-365-tenant\">Authenticating with your Office 365 tenant<\/h3>\n<p>Before we run any commands that call Office 365, we need to make sure that we are connected to our Office 365 tenant, you can do this using the <code class=\"language-plaintext highlighter-rouge\">login<\/code> command.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>PS &gt; o365 login\nTo sign in, use a web browser to open the page https:\/\/microsoft.com\/devicelogin and enter the code XXXXXXXXXX to authenticate.\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>The default login method used is the device code method, this is ideal for using accounts that have multi-factor authentication enabled on them as the login is interactive and performed in the browser.<\/p>\n<p>Simply navigate to the Microsoft device login screen, enter the code returned from the command and login using your Office 365 account.<\/p>\n<blockquote><p>Learn more information about authenticating with Office 365 CLI and other authentication methods available <a href=\"https:\/\/pnp.github.io\/office365-cli\/user-guide\/connecting-office-365\/#log-in-to-office-365\">here<\/a><\/p><\/blockquote>\n<h3 id=\"executing-commands-from-powershell\">Executing commands from PowerShell<\/h3>\n<p>Once connected to your Office 365 tenant, executing commands is straight forward.<\/p>\n<p>If we want to return information about a SharePoint Online site, we can use the <code class=\"language-plaintext highlighter-rouge\">spo web get<\/code> command, as shown in the example below.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>PS &gt; o365 spo web get --webUrl \"https:\/\/contoso.sharepoint.com\"\nTitle : My Awesome Site\nUrl   : https:\/\/contoso.sharepoint.com\n...\n<\/code><\/pre>\n<\/div>\n<\/div>\n<blockquote><p>Learn more about all the commands available <a href=\"https:\/\/pnp.github.io\/office365-cli\/cmd\/consent\/\">here<\/a><\/p><\/blockquote>\n<h3 id=\"working-with-data\">Working with data<\/h3>\n<p>In the previous example, the response is simply returned to the PowerShell prompt as text, however when building scripts we want to reuse information from previous commands to pass into other commands, or interrogate the returned responses in more detail.<\/p>\n<p>To work with the data returned from the CLI commands, we can use <code class=\"language-plaintext highlighter-rouge\">--output json<\/code> option to ask the CLI to return JSON instead of text, which is the default output type for all CLI commands, and convert that response into it into a useable object.<\/p>\n<p>PowerShell conveniently provides us with a cmdlet to do this, <code class=\"language-plaintext highlighter-rouge\">ConvertFrom-Json<\/code>, which makes coverting JSON responses into a PowerShell object easy.<\/p>\n<p>We can use a pipe (<code class=\"language-plaintext highlighter-rouge\">|<\/code>) to pass the output of the CLI command directly to this cmdlet and save it as a PowerShell variable which enables us to then access properties directly and return their values.<\/p>\n<p>If we want to access the Title property value of a SharePoint Web, building on the previous example we can use this approach, as shown in the example below.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>PS &gt; $web = o365 spo web get --webUrl \"https:\/\/contoso.sharepoint.com\" --output json | ConvertFrom-Json\nPS &gt; $web.Title\nMy Awesome Site\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>And if we wanted to iterate over a response that returns a JSON array we can pipe the PowerShell object into the <code class=\"language-plaintext highlighter-rouge\">ForEach-Object<\/code> cmdlet, as shown in the example below.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>$sites = o365 spo site list -o json | ConvertFrom-Json\n$sites | ForEach-Object { $_.Title }\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>As the above examples show, we can easily create PowerShell objects using this approach using the <code class=\"language-plaintext highlighter-rouge\">ConvertFrom-Json<\/code> cmdlet and the use the data with any PowerShell cmdlet available.<\/p>\n<blockquote><p><strong>Example Scripts<\/strong><\/p>\n<p>The Office 365 CLI web site contains a section for example scripts, with many using PowerShell.<\/p>\n<p>Checkout the examples <a href=\"https:\/\/pnp.github.io\/office365-cli\/examples\/\">here<\/a>.<\/p>\n<p>If you build a script and would like to add it our examples section, we would love to hear from you, get in touch with the project our at our GitHub <a href=\"https:\/\/github.com\/pnp\/office365-cli\">repository<\/a>.<\/p><\/blockquote>\n<h3 id=\"find-out-more\">Find out more<\/h3>\n<p>If you need more help getting started or want more details about the commands, the architecture or the project, go to <a href=\"aka.ms\/o365cli\">aka.ms\/o365cli<\/a>.<\/p>\n<p>If you see any room for improvement or suggestions, please don\u2019t hesitate to reach out to us either on <a href=\"https:\/\/aka.ms\/o365cli\">GitHub<\/a> or <a href=\"https:\/\/twitter.com\/office365cli\">@office365cli<\/a> on Twitter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to get started managing your Office 365 tenant and SharePoint Framework projects in PowerShell and PowerShell Core.<\/p>\n","protected":false},"author":69079,"featured_media":4317,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[128,11],"tags":[53],"class_list":["post-4382","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-teams","category-office-add-ins","tag-office-365"],"acf":[],"blog_post_summary":"<p>Learn how to get started managing your Office 365 tenant and SharePoint Framework projects in PowerShell and PowerShell Core.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/4382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69079"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=4382"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/4382\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/4317"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=4382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=4382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=4382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}