How To Get X Number Of Lines From Text File Using PowerShell

This KB will show you how to get X number of lines from a text file using PowerShell cmdlet.

This can be very handy when applying Exchange \ Office 365 policies and we need break the task to small chunks.

In the example below I’m only applying the cmdlet to the first 200 names in my text file:

Get-Content c:\scripts\users.txt -totalcount 200

In this example I’m applying Email Address policy to the first 200 users in the text file

Get-Content .\users.txt -totalcount 200 | Set-Mailbox -EmailAddressPolicyEnabled $true

If I would like to apply it o the next 200 I’ll use:

Get-Content .\users.txt -totalcount 400 | Set-Mailbox -EmailAddressPolicyEnabled $true

If I would like to apply to the last 200 users I’ll use:

Get-Content .\users.txt –last 200 | Set-Mailbox -EmailAddressPolicyEnabled $true