Tagpowershell

Managing Office 365 and Exchange Online with Windows PowerShell

Connect to Office 365 PowerShell
Connect to Exchange Online PowerShell
Connect to Office 365 Compliance Center PowerShell
Connecting to Skype for Business Online by using Windows PowerShell
Set up the SharePoint Online Management Shell Windows PowerShell environment

Sorurce: https://support.office.com/en-gb/article/Managing-Office-365-and-Exchange-Online-with-Windows-PowerShell-06a743bb-ceb6-49a9-a61d-db4ffdf54fa6?ui=en-US&rs=en-GB&ad=GB

Get a list of Office365 users with License attached, Primary SMTP address and Aliases

To get an list of all users that has an Office365 license assigned and with Displayname, Primary SMTP address and all other adresses registrated on the users use this one-liner in PowerShell! ๐Ÿ™‚

get-mailbox | Where-Object {$_.SKUAssigned -eq “TRUE”} | Select-Object Displayname, PrimarySMTPAddress, EmailAddresses | Export-Csv c:\temp\noah2.csv -Encoding UTF8

Allow external senders in Public folder – Office365

Public folders are set to NOT allow external senders to public folders in Office 365. This can be fixed using the following receipt:

Conncect to the Windows Azure Active Directory Module for Windows PowerShell using the following commands.
Create a placeholder for your credentials:
$LiveCred = Get-Credential

Popup box will ask for your Office 365 Global Administrator credentials.

Create a placeholder for your Powershell Session towards Exchange Online.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection

Import your Powershell Session to connect to Exchange Online.
Import-PSSession $Session

Run this command to allow all scripts:
set-executionpolicy unrestricted

Then the public folder part
Add-PublicFolderClientPermission \(Path of the Public folder name) -User Anonymous -AccessRights CreateItems

Change Default emailadress on Public folder in Office365

The problem:
A public folder in Office 365 Hosted Exchange will be assigned a @onmicrosoft.com address by default. And there is of cource no way of changing this to your primary domain.

Solution:
Conncect to the Windows Azure Active Directory Module for Windows PowerShell using the following commands.
Create a placeholder for your credentials:
$LiveCred = Get-Credential

Popup box will ask for your Office 365 Global Administrator credentials.

Create a placeholder for your Powershell Session towards Exchange Online.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection

Import your Powershell Session to connect to Exchange Online.
Import-PSSession $Session

Run this command to allow all scripts:
set-executionpolicy unrestricted

Then run the following command to disable the Policy for assigning email Adresses in public folders.
Set-MailPublicFolder -Identity “\” -EmailAddressPolicyEnabled $False

Now, in your Office 365 tennant web admin go to Exchange – Public Folders – Highlight the public folder (email enabled) – Click the pencil (edit) – Click on Email Address – highlight the address you want to use as default – Click pencil – Check the default email address checkbox.
Save, close and test.

Find unlinked GPOs in your domain

Have you ever wondered if your domain has any GPO without a link and which GPO that would be? This is how you find out with Powershell with output to the screen.


$unlinkedGPOs = (Get-GPO -all | where {([xml]($_ | Get-GPOReport -ReportType XML)).gpo.LinksTo.Length -eq 0})
Write-Host "The following GPO has no link:"
$unlinkedGPOs| ft DisplayName -HideTableHeaders
Write-Host "Total: $($unlinkedGPOs.Count)"

Prevent write-output to perform linebreak

Sometimes you want to output some text and append an variable. Problem is that write-output usually performs a linebrak when appending.


$text = "some text."
Write-Output "this is " $text

Output will be:
this is
some text.

There are two ways you can solve this. One is to append the text to the variable before doing write-output. Second is to put the variable inside the quotation marks.


#Example 1
$text = "some text."
$textmessage = "This is " + $text
Write-Output $textmessage


#Example 2
$text = "some text."
Write-Output "this is $($text)"

Find domain admins with password never expires

Do you know how many domain admins you have in your domain? Do you know which domain admins are running with “password never expires” enabled? Thanks to Powershell it is easy to find out.


# Variables below
#
$domainadmins=(Get-ADGroupMember -Identity "Domain admins" | Get-ADUser -Properties PasswordNeverexpires, lastlogondate)
#*********************************************************************************************
# Output to screen
$domainadmins | Sort -Descending -Property PasswordNeverexpires | Select-Object Name, Samaccountname, lastlogondate, Enabled, PasswordNeverExpires | ft -autosize
#

This will display on your screen all members in “Domain Admins” with the last logon time, if the account is enabled and if the “password never expires” is set.

If you want a file export you can use this line instead of output to screen:


# Output to file
$domainadmins | Sort -Descending -Property PasswordNeverexpires | Select-Object Name, Samaccountname, lastlogondate, Enabled, PasswordNeverExpires | Export-Csv C:\Temp\domainadmins.csv -Encoding UTF8 -Delimiter ","

Check for Active Directory replication failures

A quick and easy way to check your Active Directory for any replication errors using Powershell.

Get-ADReplicationPartnerMetadata -Scope Domain -Target (Domain name) | ft Server,LastReplicationSuccess,ConsecutiveReplicationFailures -AutoSize

This will list all your Domain Controllers and when they last replicated successfully and show how many consecutive replications errors they have (if any).

Configure DHCP server with cmd or powershell

I wanted to share real quick how you set up and configure a DHCP server on Windows Server 2008 R2 or 2012 (R2) with he command line or powershell.

Windows Server 2008 R2:
Let’s begin with Windows Server 2008 R2, which doesn’t have any dhcp cmdlets in powershell so we have to mostly stick to the commandline.

To install the DHCP role, open an elevated powershell window:
Import-module ServerManager
Add-windowsfeature -Name DHCP

Note that the DHCP Server service is installed but stopped and disabled. Now we have to go to an elevated command prompt for the rest.To set the service to automatic startup and start the service.
sc config dhcpserver start= auto
net start dhcpserver

Authorize the server in AD:
netsh dhcp add server Server1.dhcplab.local 10.11.12.11

Create a scope, which is empty
netsh dhcp server add scope 10.11.12.0 255.255.255.0 "Scope1"

Add a range of IP addresses for leasing
netsh dhcp server scope 10.11.12.0 add iprange 10.11.12.101 10.11.12.200

Set the default gateway option
netsh dhcp server scope 10.11.12.0 set optionvalue 003 IPADDRESS 10.11.12.254

Set the lease time in seconds
netsh dhcp server scope 10.11.12.0 set optionvalue 51 DWORD 28800

Set the DNS server
netsh dhcp server scope 10.11.12.0 set optionvalue 006 IPADDRESS 10.11.12.1

Add a DHCP reservation, for a printer in this case
netsh dhcp server scope 10.11.12.0 add reservedip 10.11.12.190 A1B2C3D4E5F6 "Printer1" "Reservation for Printer1"

Windows Server 2012
Everything in an elevated powershell window. How sweet it is! ๐Ÿ™‚

Install the DHCP Role (no need to configure the service afterwards)
add-windowsfeature -Name DHCP -includemanagementtools

Authorize the DHCP server
Add-DhcpServerInDC

Add scope with IP range for leasing
Add-DhcpServerv4Scope -Name "Scope2" -StartRange 10.11.12.101 -EndRange 10.11.12.200 -SubnetMask 255.255.255.0

Set lease duration (days.hours:minutes:seconds)
Set-DhcpServerv4Scope -ScopeId 10.11.12.0 -LeaseDuration 1.00:00:00

Set default gateway
Set-DhcpServerv4OptionValue -ScopeId 10.11.12.0 -Router 10.11.12.254

Set dns server and domain name
Set-DhcpServerv4OptionValue -DnsServer 10.11.12.1 -DnsDomain dhcplab.local

Set IP reservation, again for a printer
Add-DhcpServerv4Reservation -ScopeId 10.11.12.0 -IPAddress 10.11.12.190 -ClientId A1-B2-C3-D4-E5-F6 -Description "Reservation for Printer1" -Name "Printer1"

Powershell: Exchange – List all mailboxes in one database

To list all mailboxes in one database and sort it by size, use this nice PowerShell line:

Get-MailboxStatistics -Database <DATABASENAME>ย | select DisplayName, ItemCount, TotalItemSize | Sort-Object TotalItemSize -Descending

 

If you want to export the list to an CSV file, put “| Export-CSV C:\MailBoxSize.CSV”ย at the end of the PowerShell line.

© 2020 IdefixWiki

Theme by Anders NorénUp ↑