{"id":1201,"date":"2013-12-05T22:13:47","date_gmt":"2013-12-05T22:13:47","guid":{"rendered":"http:\/\/dollarunderscore.azurewebsites.net\/?p=1201"},"modified":"2014-05-02T19:26:22","modified_gmt":"2014-05-02T19:26:22","slug":"office-365-powershell-toolbox-oneliners-to-rule-them-all","status":"publish","type":"post","link":"https:\/\/p0wershell.com\/?p=1201","title":{"rendered":"Office 365 PowerShell Toolbox &#8211; Oneliners to rule them all!"},"content":{"rendered":"<p>In this post, I will try to add some of the commands you need for managing Office 365, and some code snipets to do pre-migration checks and so on.<\/p>\n<p>I will edit this post and keep adding things to keep them in one place.<\/p>\n<p><strong>Connecting to MSOL\/Exchange Online<\/strong><\/p>\n<p>To connect to MSOL you simple run:<br \/>\n[PowerShell]<br \/>\nConnect-MsolService<br \/>\n[\/PowerShell]<\/p>\n<p>To connect to Exchange Online:<br \/>\n[PowerShell]<br \/>\n$UserCredential = Get-Credential<\/p>\n<p>$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:\/\/ps.outlook.com\/powershell\/ -Credential $UserCredential -Authentication Basic -Allow<\/p>\n<p>Import-PSSession $Session<br \/>\n[\/PowerShell]<\/p>\n<p>If you you are behind a proxy-server, you might need to add some session options. Like this (if you configured proxy in IE):<br \/>\n[PowerShell]<br \/>\n$proxysettings = New-PSSessionOption -ProxyAccessType IEConfig<br \/>\n$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:\/\/ps.outlook.com\/powershell -Credential $UserCredential -Authentication Basic -Allow -SessionOption $proxysettings<br \/>\n[\/PowerShell]<\/p>\n<p>To save your credentials to be used in an automated script, you can do this:<br \/>\n[PowerShell]<br \/>\n# Get the credential into a variable<br \/>\n$MyCredential = Get-Credential<\/p>\n<p># Convert the password to an encrypted string and save to file<br \/>\n$MyCredential.Password | ConvertFrom-SecureString | Out-File .\\Password.txt<\/p>\n<p># To load the credential&#8230;<\/p>\n<p># Set your username<br \/>\n$User = &#8220;john.doe@contoso.com&#8221;<\/p>\n<p># Get the password from the file<br \/>\n$Password = Get-Content .\\Password.txt | ConvertTo-SecureString<\/p>\n<p># Build the credential<br \/>\n$O365Credential = New-Object System.Management.Automation.PsCredential($User,$Password)<\/p>\n<p>[\/PowerShell]<\/p>\n<p>That credential can now be used togheter with the Connect-MsolService\/New-PSSession cmdlets.<\/p>\n<p><strong>List all users and their AccountSkuId\/License<\/strong><br \/>\nThis might sound easy, but the &#8220;Get-MsolUser&#8221;-cmdlet is returning an advanced object which makes it a bit difficult to export to for example a csv-file.<\/p>\n<p>But if you use the Select-Object-cmdlet togheter with an expression you will get the job done!<\/p>\n<p>You could to something like this to expand the AccountSkuId for all your users:<\/p>\n<p>[PowerShell]<br \/>\nGet-MsolUser -All | Select-Object UserPrincipalName, @{Name=&#8221;License&#8221; ; Expression={ ($_ | select -ExpandProperty Licenses | select -ExpandProperty AccountSkuId ) } }<br \/>\n[\/PowerShell]<\/p>\n<p>Just pipe that to for example Export-Csv to save the information on disk.<\/p>\n<p><strong>Duplicates in forwarding address<\/strong><\/p>\n<p>If you have multiple smtp-domains, you might run into a problem with duplicates if you only have one *.onmicrosoft.com domain (simpler than creating multiple ones). Want to see if this is a problem in your domain?<\/p>\n<p>This is one way of doing it:<br \/>\n[PowerShell]<br \/>\n$Duplicates=Get-ADUser -Filter * | Group-Object { ($_.UserPrincipalName -split &#8220;@&#8221;)[0] } | Where-Object Count -gt 1<br \/>\n[\/PowerShell]<\/p>\n<p>$Duplicates now contains all of the users that will be a problem if you only have one &#8220;forwarding domain&#8221;. There are many options to solve this, forward to something else (AccountName or similiar), add a part of the maildomain left of the @-sign etc&#8230;<\/p>\n<p><strong>Managing Mobile Devices<\/strong><\/p>\n<p>To add a device for a user:<br \/>\n[PowerShell]<br \/>\nSet-CASMailbox -Identity &#8216;john.doe@contoso.com&#8217; -ActiveSyncAllowedDeviceIDs ($MyArrayWithDeviceIDs)<br \/>\n[\/PowerShell]<\/p>\n<p>To get all device id&#8217;s associated with a user:<br \/>\n[PowerShell]<br \/>\nGet-CASMailbox -Identity &#8216;john.doe@contoso.com&#8217; | select -ExpandProperty ActiveSyncAllowedDeviceIDs<br \/>\n[\/PowerShell]<\/p>\n<p>Clear all allowed devices:<br \/>\n[PowerShell]<br \/>\nSet-CASMailbox -Identity &#8216;john.doe@contoso.com&#8217; -ActiveSyncAllowedDeviceIDs $null<br \/>\n[\/PowerShell]<\/p>\n<p><strong>Setting a license<\/strong><\/p>\n<p>To give a user a license you could do something like this:<\/p>\n<p>[PowerShell]<br \/>\n# Create the license options (if you need to disable some plans)<br \/>\n$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId -DisabledPlans $DisabledPlans<\/p>\n<p># Set the country (two letter &#8220;code&#8221;)<br \/>\nSet-MsolUser -UserPrincipalName $UserAccount -UsageLocation $UsageLocation<\/p>\n<p># Set the license<br \/>\nSet-MsolUserLicense -UserPrincipalName $UserAccount -AddLicenses $AccountSkuId -LicenseOptions $LicenseOptions<\/p>\n<p># If you don&#8217;t need to disable anything just use this instead of the above:<br \/>\nSet-MsolUserLicense -UserPrincipalName $UserAccount -AddLicenses $AccountSkuId<\/p>\n<p>[\/PowerShell]<\/p>\n<p><strong>Creating Shared Mailboxes<\/strong><br \/>\nTo create a shared mailbox, you first have to give it a temporary license to create the mailbox (see above), then set it to shared, remove the license and add the permissions that are needed.<\/p>\n<p>If the mailbox has a license you could do something like this:<\/p>\n<p>[PowerShell]<br \/>\n# Set it to shared (mailbox need to exist first, so set a license, wait, and then try this)<br \/>\nSet-Mailbox &#8216;john.doe@contoso.com&#8217; -Type Shared -ProhibitSendReceiveQuota 5GB -ProhibitSendQuota 4.75GB -IssueWarningQuota 4.5GB<\/p>\n<p># Set full access permissions<br \/>\nAdd-MailboxPermission &#8216;john.doe@contoso.com&#8217; -User &#8216;GroupOrUserName&#8217; -AccessRights FullAccess -Confirm:$false<\/p>\n<p># Set &#8216;SendAs&#8217; permission if needed<br \/>\nAdd-RecipientPermission &#8216;john.doe@contoso.com&#8217; -Trustee &#8216;GroupOrUserName&#8217; -AccessRights SendAs -Confirm:$false<\/p>\n<p># Remove the license<br \/>\nSet-MsolUserLicense -UserPrincipalName &#8216;john.doe@contoso.com&#8217; -RemoveLicenses $AccountSkuId<\/p>\n<p>[\/PowerShell]<\/p>\n<p><strong>Change UPN of a licensed user<\/strong><br \/>\nIf the user UPN prefix (left of @) changes, DirSync will fix it automatically, if the domain part change, you need to run a few commands to change it.<\/p>\n<p>The overall process is to change it in the On-Prem AD, change it in Azure to your &#8220;*.onmicrosoft.com&#8221;-domain, and then change it to the new domain.<\/p>\n<p>Example, we need to change John Doe&#8217;s smtp domain from &#8220;contoso.com&#8221; to &#8220;contoso2.com&#8221;:<\/p>\n<p>[PowerShell]<\/p>\n<p># Set the new UPN in Active Directory<br \/>\nSet-Aduser -identity JohnDoe -UserPrincipalName &#8216;john.doe@contoso2.com&#8217;<\/p>\n<p># Change the UPN in Azure to a temporary one<br \/>\nSet-MsolUserPrincipalName -UserPrincipalName &#8216;john.doe@contoso.com&#8217; -NewUserPrincipalName &#8216;john.doe@contoso.onmicrosoft.com&#8217;<\/p>\n<p># Change it to the new one<br \/>\nSet-MsolUserPrincipalName -UserPrincipalName &#8216;john.doe@contoso.onmicrosoft.com&#8217; -NewUserPrincipalName &#8216;john.doe@contoso2.com&#8217;<\/p>\n<p>[\/PowerShell]<\/p>\n<p>Run a DirSync and you are done!<\/p>\n<p><strong>Add a Room<\/strong><br \/>\nTo add a room mailbox and set it to auto-accept a booking (if the time slot is free), and make it possible for your users to book it a year ahead (for example) you do the following:<\/p>\n<p>[PowerShell]<\/p>\n<p># Create the room (with seats for 20 people)<br \/>\nNew-Mailbox -Name &#8220;ConferenceRoom1&#8221; -DisplayName &#8220;Conference Room 1&#8221; -PrimarySmtpAddress &#8220;ConferenceRoom1@contoso.com&#8221; -Office &#8220;Contoso HQ&#8221; -ResourceCapacity 20 -Room<\/p>\n<p># Make it accept invitations if the time slot is free,<br \/>\nSet-CalendarProcessing &#8220;ConferenceRoom1&#8221; -AutomateProcessing AutoAccept -BookingWindowInDays 365<\/p>\n<p>[\/PowerShell]<\/p>\n<p>If you run Outlook 2010 or newer (or the OWA) you most certainly want to create a roomlist. The users can then pick that list and see all available rooms right away. You might want to name it after the Office location or similar.<\/p>\n<p>To add a room list and add a room to that list you do the following:<\/p>\n<p>[PowerShell]<\/p>\n<p># Add the room list<br \/>\nNew-DistributionGroup -Name &#8220;ContosoHQ-Rooms&#8221; -DisplayName &#8220;Contoso HQ&#8221; \u2013PrimarySmtpAddress &#8220;contosoHQ-rooms@contoso.com&#8221; \u2013RoomList<\/p>\n<p># Add a room to it<br \/>\nAdd-DistributionGroupMember \u2013Identity &#8220;ContosoHQ-Rooms&#8221; -Member &#8220;ConferenceRoom1&#8221;<\/p>\n<p>[\/PowerShell]<\/p>\n<p>You might need to update your offline address book before this works properly in Outlook, it should work pretty much instantly in the OWA.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I will try to add some of the commands you need for managing Office 365, and some code snipets to do pre-migration checks and so on. I will edit this post and keep adding things to keep them in one place. Connecting to MSOL\/Exchange Online To connect to MSOL you simple run: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[111,21],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3Zj0A-jn","_links":{"self":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts\/1201"}],"collection":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1201"}],"version-history":[{"count":0,"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts\/1201\/revisions"}],"wp:attachment":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}