Most administrative functions in SharePoint Online work with the user interface. However, there are times when you perform repetitive tasks that can be coded with PowerShell. For example, organizations often need to provide security and compliance officers with reports on current permissions, site structure, and even site content. This can't easily be done from the UI, but with PowerShell it's very easy and can be run multiple times if needed.
In these situations, PowerShell is the tool of choice as it allows remote management of all Office 365 components. For those new to PowerShell, it is a task automation and configuration management framework from Microsoft that is being developed to make some of your tasks much, much easier. In this post, we walk through the steps required to work with SharePoint Online using PowerShell.
However, there is a difference between SharePoint Online PowerShell commands and Office or Microsoft 365 PowerShell commands.
SharePoint OnlinePowerShell commands only manage SharePoint Online users, sites, services, and components. This oneOffice 365PowerShell commands manage tasks at the Office and Microsoft 365 level, such as managing users, licenses, organization information, and core services at the tenant level.
Both sets of PowerShell commands can manage users and groups at the SharePoint level or at the tenant level.
Office 365 PowerShell setup for remote administration
The first step is to configure your system and prepare it. Download the following components:
- Microsoft Online Service Sign In Assistant for IT Professionals
- Módulo Windows Azure Active Directory para Windows PowerShell
- SharePoint Online Management Shell
Load each component intoorder aboveto ensure each one was successfully installed before moving on to the next one. Make sure you are using the correct version based on your operating system (32-bit or 64-bit). Currently supported operating systems are Windows 10, Windows 7 Service Pack 1, Windows 8.1, Windows Server 2008 R2 SP1, Windows 10 Version 1507, 1511, Windows 10 Version 1607 and later, Windows Server 2012, Windows Server 2016 and Windows Server 2019.
Now launch a standard PowerShell window and run the following command:
Set-ExecutionPolicy RemoteSigned
This step ensures that any PowerShell scripts you run are not blocked by PowerShell security measures.
You are now set up as an administrator and can use Office or Microsoft 365 PowerShell commands. You will notice that there is no installation for Exchange Online PowerShell cmdlets. The connection to Exchange Online is completed through theNova-PSSessioneImport-PSSessioncommands instead.
Connect to Office or Microsoft 365 using PowerShell
To run the necessary PowerShell commands, use the PowerShell ISE tool on Windows for convenience. To launch this if you are using Windows 8 and above just press on start menu and “PowerShell-ISE' and launch it from there. Once found, right-click on it and select "Execute as administrator“.
Once PowerShell ISE loads, you can run commands in the scripts section of the PowerShell ISE window and see the execution and results in the bottom half. If the top section is not visible, click "road map” and it will split the screen into two sections.
In the script pane, you can now initiate a connection to Office or Microsoft 365 by typing the following PowerShell commands in order. The first command consists of a variable and a get-credential command that allows you to securely enter your Microsoft credentials and create a variable that can be used in other commands when authentication is required.
$credential = Get-Credential
If prompted, enter your username and password in the pop-up window, after entering them pressOK, which closes the dialog box and populates the credential variable.
To validate the variable, in the blue section at the bottom of thePowerShell-ISE, just enter$credentials, then press Enter and the created login value will be displayed.
After saving and making the credentials available, you need to import the PowerShell modules directly into the PowerShell ISE window. This is done by typing:
Import-Module MSOnline
Once it loads, you can use the saved credential object to initiate a connection to Office or Microsoft 365 with the following command:
Connect-MsolService -Credencial $credencial
Unless you see any errors, you are now connected to Azure Active Directory. To connect to SharePoint Online, you need to run the following command which imports SharePoint Online PowerShell cmdlets.
Import-Modul Microsoft.Online.Sharepoint.PowerShell
When this is complete, you will have access to all modules and cmdlets in your current PowerShell session. You can now connect to SharePoint Online by pasting:
Connect-SPOService –URLhttps://{mandant}-admin.sharepoint.com -Credential $credential
PowerShell-Cmdlets do SharePoint Online
The SharePoint Online Administration PowerShell Pack that you installed earlier provides the PowerShell cmdlets an administrator would need to manage SharePoint Online. The complete list of cmdlets supported by SharePoint Online can be found here:
https://technet.microsoft.com/en-us/library/fp161364.aspx
The following table lists the most commonly used cmdlets.
Cmdlet-Name | Description |
Add-SPOUser | Adds an existing Office 365 user or Office 365 security group to a SharePoint group. |
Connect-SPOService | Connects a SharePoint Online global admin to a SharePoint Online connection (the SharePoint Online admin center). This cmdlet must run before other SharePoint Online cmdlets can run. |
Disconnect-SPOService | Disconnects from a SharePoint Online service. |
Get-SPOExternalUser | Returns external users located in the tenant's folder based on specified criteria. |
Get-SPOSite | Returns one or more site collections. |
Get-SPOSiteGroup | Returns all groups in the specified site collection. |
Get-SPOTenant | Returns the properties of the SharePoint Online organization. |
Get-SPOUser | Returns the SharePoint Online user or security group accounts that match the specified search criteria. |
Neue-SPOSite | Creates a new SharePoint Online site collection for the current company. |
Neue-SPOSiteGroup | Creates a new group in a SharePoint Online site collection. |
Remove SPOSite | Sends a SharePoint Online site collection to the SharePoint Online Recycle Bin. |
Remove-SPOSiteGroup | Removes a SharePoint Online group from a site collection. |
Remover-SPOUser | Removes a user or security group from a site collection or group. |
Set-SPOUser | Set properties for an existing user. |
Test-SPOSite | Tests a SharePoint Online site collection. |
Upgrade-SPOSite | Starts the process of upgrading a site collection. |
Certain management tasks require the use of a number of different cmdlets, not just those used in SharePoint Online. Combining multiple cmdlets allows management of services across the tenant. An example of this is when you want to provision a user directly in Office 365. You cannot use the SharePoint Online cmdlets for this, you must use the core cmdlets from Azure Active Directory first.
Manage SharePoint Online with PowerShell
The most common tasks performed using PowerShell in SharePoint Online are related to the following features:
- User permissions and security
- Update user details
- Creating, updating and removing websites
- list interactions
To use PowerShell cmdlets, you must understand the SharePoint objects and properties that are retrieved by running the commands.
Recovering SharePoint sites
Get-SPOSite
Get a list of groups (SP groups)
Get-SPOSite | ForEach-Object {Get-SPOSiteGroup -Site $_.Url} |Format table
Get a list of all users
Get-SPOSite | ForEach-Object {Get-SPOUser -Site $_.Url}
Retrieving information from SharePoint is done through the use of "TO RECEIVE” with the various parameters added as needed. However, the process of setting values is different as it involves using the "DEFINED AS' commands.
Designate a new site collection administrator
Set-SPOUser -Site {SharePoint Online Site} -LoginName user@domain.com -IsSiteCollectionAdmin $true
Remove a user from the site collection administrators group
Remove-SPOUser -LoginName user@domain.com -Site {SharePoint Online-Site}
The real power comes from combining these commands with regular PowerShell functions to create reusable scripts that can be run manually or as scheduled tasks. For example, a common administrative task is to get all sites along with their security groups and memberships. And the following is an effective way to do this:
// Get all SharePoint Online sites
$sites = Get-SPOSite
// Go through all sites one by one
foreach ($site em $sites)
{
Write-Host $site.Url -ForegroundColor „Gelb“
// Get groups within the site
$groups = Get-SPOSiteGroup -Site $site.Url
// Iterate over all groups
foreach($group in $groups)
{
// Get specific site
$siteGroup = Get-SPOSiteGroup -Site $site.Url -Grupo $group.Title\
Write-Host $siteGroup.Title -ForegroundColor "Ciano"
// Get the members of the selected group
$siteGroup | Select-Object -ExpandProperty-Benutzer
recording host
}
}
This PowerShell repeats all sites, groups, and members in SharePoint Online. It can be saved as a "ps1" file and run at any time, manually or as a scheduled task. This makes PowerShell the easiest and most efficient way to perform repeatable tasks.
Finally, Office 365 cmdlets allow for quick and easy management of not only SharePoint Online, but also the full functionality of Azure Active Directory, Exchange, Skype and SharePoint. Learning these cmdlets and becoming familiar with PowerShell in general will provide the best approach to management tasks.
FAQs
How PowerShell is used in SharePoint? ›
- Go to Start >> Type “PowerShell ISE”.
- Right-click and Open PowerShell ISE with “Run as Administrator” if you have UAC enabled.
- Now, You can start writing your PowerShell script or copy-paste the script and then click on the “Run Script” button from the toolbar. (
- Step 1: Install the SharePoint Online Management Shell or SharePoint Online PowerShell Module. ...
- Step 2: Connect to SharePoint Online PowerShell using the Connect-SPOService cmdlet. ...
- Step 3: Start using SharePoint Online PowerShell cmdlets!
- PowerShell requires.NET framework.
- Object-Based: With most shells, text-based commands are used to get the job done while writing scripts. ...
- Security Risks: Another potential drawback of using Windows PowerShell is that it can create some potential security risks.
To get started using PowerShell to manage SharePoint Online, you need to install the SharePoint Online Management Shell and connect to SharePoint Online. Install the SharePoint Online Management Shell by downloading and running the SharePoint Online Management Shell or installing the module from the PowerShell Gallery.
What are 3 benefits of PowerShell? ›...
Why Is PowerShell Used?
- Enabling task automation. ...
- Driving data accessibility. ...
- Managing “infrastructure as code” ...
- Facilitating remote commands.
- Step 1: Create an Azure Automation Account. If you don't have any existing Azure automation accounts created, you must create one first. ...
- Step 2: Import Necessary PowerShell Modules. ...
- Step 3: Add Credentials for Run as Account. ...
- Step 4: Create a Runbook with PowerShell Script. ...
- Step 5: Add a Schedule to your PowerShell Script.
To get a document library using PowerShell, you will first need to connect to your SharePoint Online site using the Connect-PnpOnline cmdlet. Once you have connected, you can use the Get-PnPList cmdlet to get your document library.
How do I enable SharePoint feature in PowerShell? ›Well, To activate a site collection feature in SharePoint, navigate to Site settings >> Site Collection Features >> Click on “Activate” next to the relevant feature. How to Activate a Feature using PowerShell? To activate a SharePoint feature on a particular site, we use Enable-SPFeature cmdlet.
What is PowerShell most commonly used for? ›As a scripting language, PowerShell is commonly used for automating the management of systems. It is also used to build, test, and deploy solutions, often in CI/CD environments. PowerShell is built on the . NET Common Language Runtime (CLR).
Why do hackers use PowerShell? ›PowerShell was used to carry out the critical piece of the attack. The PowerShell script was used to disable Windows Defender's antivirus prevention capabilities like real-time detection, script and file scanning and a host-based intrusion prevention system.
What cool things can you do with PowerShell? ›
The uses of PowerShell include adding and deleting accounts, editing groups, and creating listings to view specific types of users or groups. You can also choose to use the Windows PowerShell Integrated Scripting Environment (ISE), a graphic user interface that lets you run commands and create or test scripts.
When should you not use SharePoint? ›- 1 Cost. There are so many things that SharePoint can do beyond file storage. ...
- 2 Poor Usability. ...
- 3 Document Visibility. ...
- 4 Document Duplication Woes. ...
- 5 Data Integrity. ...
- 6 URL Issues. ...
- 7 Navigation. ...
- 8 Can't Sort or Filter.
No more than 100 GB total file size. No more than 30,000 files. Each file must be less than 15 GB.
Is Microsoft doing away with SharePoint? ›Therefore, we've made the difficult decision to discontinue the SharePoint Online Public Website feature so that we can focus our efforts and investments on delivering capabilities in Microsoft 365 that will bring more value to our customers.
What is the most important PowerShell command? ›- Get-Process.
- Stop-Process.
- Get-Service.
- Get-History.
- Start-Job.
- ConvertTo-HTML.
- Out-File.
- Export-CSV.
There are few differences in the PowerShell language between Windows PowerShell and PowerShell. The most notable differences are in the availability and behavior of PowerShell cmdlets between Windows and non-Windows platforms and the changes that stem from the differences between the . NET Framework and . NET Core.
Is PowerShell worth learning 2022? ›You should learn it for the following reasons: PowerShell can be used to control all of Microsoft's server products right now. Manually performing operations such as updating an active directory can take hours. You may complete it in less time by utilizing PowerShell and a single command.
Is PowerShell good for automation? ›With PowerShell you can sequentially execute multiple commands at once or pipe output commands to automate common tasks. Designed for app makers and administrators to automate tasks with environments and associated apps, flows, and connectors.
How do I upload a file to SharePoint using PowerShell? ›Navigate to your SharePoint Online library >> Click on “Upload” from the toolbar and choose the “Folder” option. Browse and select a folder from your computer. You can also upload a folder with files and sub-folders just by dragging and dropping! PowerShell can also be used to upload a folder to SharePoint Online.
What tasks can you automate with PowerShell? ›PowerShell can be used to automate tasks such as user management, CI/CD, managing cloud resources and much more. You'll learn to run commands, how to learn more about PowerShell and additionally to create and run script files.
What does F7 do in PowerShell? ›
Pressing the F7 function key presents a command history menu. Use the arrow keys to change the selection in the menu. Pressing Enter executes the command selected.
How to download file from SharePoint Online using PowerShell? ›...
How to Download a File from SharePoint Online Library?
- Sign-in to the SharePoint Online site >> Navigate to the library where your desired file is located.
- Right-click on the file and select the “Download” option from the context menu.
- Step 1: Connect to SharePoint Online Site. To start with, we need the connection context to the SharePoint Online site. ...
- Step 2: Get List Items from the SharePoint Online List. ...
- Step 3: Loop through the List Items Collection to Read List Items.
How Long Does it Take to Learn PowerShell? PowerShell is a powerful command-line interface solution for Windows devices. As such, it usually takes around one to two weeks to get a handle on it.
Which of the following basics you should know about PowerShell? ›- Prerequisites.
- Getting Help Information on Commands with Get-Help.
- Retrieving Computer Processes with Get-Process.
- Fetching PowerShell Session History with Get-History.
- Displaying System Services with Get-Service.
- Running Background Jobs with Start-Job.
The primary qualifications for becoming proficient in PowerShell include experience and a willingness to learn. While the time needed to become proficient varies based on factors like how much you are using it, most people with a technical background can become reasonably proficient after several months of practice.
What does $() mean in PowerShell? ›Subexpression operator $( )
For a single result, returns a scalar. For multiple results, returns an array. Use this when you want to use an expression within another expression. For example, to embed the results of command in a string expression. PowerShell Copy.
...
Keyboard shortcuts for running scripts.
Action | Keyboard Shortcut |
---|---|
Open | CTRL + O |
Run | F5 |
Run Selection | F8 |
Stop Execution | CTRL + BREAK . CTRL + C can be used when the context is unambiguous (when there is no text selected). |
Powershell enables you to manage permissions without having to deal with a mix of standard CMD and GUI tools. You get an easy way to manage permissions that are rule based rather than hierarchical. Once you get used to automating file system problems, you end up moving away from the GUI.
How many PowerShell commands are there? ›Over 200 cmdlets can be used in PowerShell. Windows PowerShell command prompt isn't case-sensitive, so these commands can be typed in either upper or lower case.
Why is PowerShell so powerful? ›
Microsoft's PowerShell is a cross-platform configuration management framework that enables the seamless administration of various managed elements of computing objects. It is most commonly used to automate systems management and to build, test, and deploy solutions in CI/CD environments.
Can you be hacked through PowerShell? ›Most security software whitelists PowerShell and treats it as a trusted application, which makes it ripe for hacking. A hacker can run fileless malware in the system's memory as opposed to downloading it on a local machine.
Can you get hacked through PowerShell? ›PowerShell is a powerful tool for system administration; as such, it is also the perfect entry point for hackers. Due to PowerShell's tight integration into the system, attempts to simply block it provide a false sense of security. The best protection is provided by PowerShell's own mechanisms.
Why is PowerShell a security risk? ›The factors that make Microsoft PowerShell valuable to IT admins, such as remotely administering and diagnosing a PC, also make it useful to attackers. Many attackers, including ransomware threat actors, use PowerShell as a post-exploitation tool.
Is PowerShell a valuable skill? ›PowerShell can be one of the most effective tools administrators have for managing Windows systems. But it can be difficult to master, especially when time is limited. An online PowerShell course can expedite this process by prioritizing the most important topics and presenting them in logical order.
What does @{} mean in PowerShell? ›To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol.
Is PowerShell difficult to learn? ›PowerShell isn't that hard to learn. It is flexible and powerful, but is hard to master because if what it does. As a language the syntax is fairly easy. I could teach you the basic syntax in just a few hours (assuming you had some computer concepts like understanding what a variable was).
How to get data from SharePoint list using PowerShell? ›- Step 1: Connect to SharePoint Online Site. To start with, we need the connection context to the SharePoint Online site. ...
- Step 2: Get List Items from the SharePoint Online List. ...
- Step 3: Loop through the List Items Collection to Read List Items.
As a scripting language, PowerShell is commonly used for automating the management of systems. It is also used to build, test, and deploy solutions, often in CI/CD environments. PowerShell is built on the . NET Common Language Runtime (CLR).
How to install SharePoint module in PowerShell? ›- Step 1: Import SharePoint Online PowerShell module.
- Step 2: Connect to SharePoint Online from PowerShell.
- Step 3: Run PowerShell cmdlets for SharePoint Online.
How do I download a file from SharePoint using PowerShell? ›
To download all files from a SharePoint Online document library, browse to your SharePoint document library >> Select all Files and folders. Click on the “Download” button from the toolbar. You can use the “File Explorer view” or PowerShell as alternative options to download the document library.
How do I update an item in SharePoint list using PowerShell? ›To update the item, simply use the Set-PnPListItem cmdlet, specifying the ID of the list item and the new values for the fields you wish to update. This cmdlet will automatically save your changes back to SharePoint.
How to learn PowerShell scripting easily? ›- Basic familiarity with using a command-line shell like Command Prompt or Git Bash.
- Visual Studio Code.
- Ability to install Visual Studio Code extensions.
- Ability to install software on your computer, if you're not using a Windows operating system.
- Familiarity with running commands in PowerShell.
The uses of PowerShell include adding and deleting accounts, editing groups, and creating listings to view specific types of users or groups. You can also choose to use the Windows PowerShell Integrated Scripting Environment (ISE), a graphic user interface that lets you run commands and create or test scripts.
How to check user permissions in SharePoint Online PowerShell? ›- Step 1: Install the SharePoint Online SDK file from Microsoft. Make sure you have client. ...
- Step 2: Load SharePoint Windows PowerShell Snap-in. Once you install above, go to the Start Menu, and open “Windows PowerShell” ...
- Step 3: Add 'DLL Libraries' needed. ...
- Step 3: Connect to SharePoint Online:
Run Netwrix Auditor. Navigate to “Reports” -> Click “Predefined” -> Expand the “SharePoint Online” section -> Go to “SharePoint Online – State-in-Time” -> Select “SharePoint Online Object Permissions” -> Click “View”. Specify the site URL in the “Object Path” field -> Click “View Report”.
What is SharePoint management Shell? ›The SharePoint Online Management Shell is a tool that contains a Windows PowerShell Module to manage your SharePoint Online subscription in the Office 365.
What is the fastest way to upload files to SharePoint? ›- Open the OneDrive or SharePoint site library.
- Select Upload at the top of the Documents library.
- In the Add a document dialog box, select Browse to upload an individual file. ...
- When you've selected the file or files to upload, select OK.
To get a document library using PowerShell, you will first need to connect to your SharePoint Online site using the Connect-PnpOnline cmdlet. Once you have connected, you can use the Get-PnPList cmdlet to get your document library.
How do I automatically upload files to SharePoint? ›Make A Flow To Upload Documents To A SharePoint Library
Open the Power Automate action from the top menu and select Create a new flow. Choose the Power Apps button template. Name the flow UploadFileToDocument library and click Save.