Thursday, January 24, 2019

OneDrive For Business: Provisioning via PnP PowerShell

Let's assume you want to migrate stuff from a personal folder, a file share or OneDrive to a tenant. Now, let's also assume that the user never logged in and therefore has no personal site. How do you migrate the data to the users personal site? Well, you just create it for the user!

With this PnP PowerShell script, you can simply create the personal sites & OneDrive for Business accounts for users in your environment. Simply log in to O365 with your SharePoint address (f.ex. and run this script
The first script is for multiple users, the second script is for a single user.

Monday, January 21, 2019

PowerShell: Hide Libraries in SharePoint

This is a simple one: hide libraries in SharePoint. You can either use the version with parameters or the one without.

Thursday, January 10, 2019

PowerShell: Remove Items & Document Sets from Library

Here are the basics of the script:
- Get your web
- Get your list, you can also use a foreach and iterate through every list
- From the list get your root folder
- The root folder has multiple sub folders, which are the document sets
- Iterate through each document set / sub folder and delete the items
- Delete the sub folder
- Move to the next sub folder
- Important to note: I excluded the "Forms" folder, because you can't delete that
I just threw this together in a couple of minutes, so expect errors. I'm also redoing the whole "foreach" thing, because I'm changing the collection with every delete.

Wednesday, January 2, 2019

PowerShell: Create a Password file

In my Azure Functions posts, I'm using a Password file which contains the password a service account. So here's how I'm creating the files needed:

Azure Functions, PowerShell and how to add users from a SharePoint List

In posts before (see here and here) I explained how to create a HTTPTrigger PowerShell Azure Function. How can we use that now? Well, one thing could be to create users in Azure AD from a SharePoint list. We need to create a SharePoint list with a couple of fields and also create the Azure Function for that. And here's how we will be doing this.

You can either click in the UI until you have a list with these fields:
  • Internalname: Type
  • UPN: Text
  • UserManager: User
  • Department: Choice
  • GivenName: Text
  • SurName: Text
  • Jobtitle: Choice
  • UsageLocation: Choice
  • License: Choice
  • MailAddress: Text

OR! We can use SharePoint PnP PowerShell and do this quicker:

Now that we have a list, we can put in some entries. You don't need to do that now, we'll have to enter some people later on. We will create create a Flow that can trigger our Azure Function:

Click on "Platform features" and select "API definition"

On the next screen click on "Generate API definition template":

Azure will create the definition, as can be seen here:

Click on "Save" and then on "Export to PowerApps + Flow":

Configure the custom API:
After that, switch over to Flow, create a new template and search for your API:

After we did this, we can go back to the good stuff: PowerShell. Our header of the function will look like this:

The Flow will provide us with an ItemID ($itemID), a URL($url) and the list title (listTitle). We will need them to get the user from the list and pull information from the list.
In this next part we will define a lot of things:
  • $FunctionName: This needs to be name you were giving the function, in this example it will be "AddAzureADUser"
  • Define modules: We need three modules (SharePointPnPPowerShellOnline, AzureAD, MSOnline), change the version numbers to your versions
  • Define username and password: in this example they are stored as environmental variables in the Azure Function
  • Importing PowerShell modules: the modules are stored in "bin", see this blog post
  • Build credentials: from the stored information, we're creating the credential object
  • Tenant ID: You will have to provide your tenant ID
  • Conect via MSOL Service
  • Connect via PnP Online
  • Connect via Azure AD

During this part, we are creating password profile so that the user has to change the password on the first login.
If there is a MailAdress definied, we will split that and create a MailNickName.
And lastly we are creating the user.

If the user has an e-Mail address in the user list, we are storing the e-Mail address in the Azure AD and the SharePoint User Profile Service
With the created user, we can also set some information for that person.
In this part we are changing the department, the given name, the surname, the jobtitle and the usage location. You have to set the usage location! Also we are doing this for Azure AD and the SharePoint User Profile Service

Finally we are setting the license! It is important that you replace the "TENANTNAME:" with your own tenant name.

Here are the final scripts: