Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Exchange Server 2016 PowerShell Cookbook

You're reading from   Microsoft Exchange Server 2016 PowerShell Cookbook Powerful recipes to automate time-consuming administrative tasks

Arrow left icon
Product type Paperback
Published in Jul 2017
Publisher
ISBN-13 9781787126930
Length 648 pages
Edition 4th Edition
Languages
Arrow right icon
Authors (4):
Arrow left icon
Mike Pfeiffer Mike Pfeiffer
Author Profile Icon Mike Pfeiffer
Mike Pfeiffer
Nuno Filipe M Mota Nuno Filipe M Mota
Author Profile Icon Nuno Filipe M Mota
Nuno Filipe M Mota
Nuno Mota Nuno Mota
Author Profile Icon Nuno Mota
Nuno Mota
Jonas Andersson Jonas Andersson
Author Profile Icon Jonas Andersson
Jonas Andersson
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. PowerShell Key Concepts FREE CHAPTER 2. Exchange Management Shell Common Tasks 3. Managing Recipients 4. Managing Mailboxes 5. Distribution Groups and Address Lists 6. Mailbox Database Management 7. Managing Client Access 8. Managing Transport Servers 9. Exchange Security 10. Compliance and Audit Logging 11. High Availability 12. Monitoring Exchange Health 13. Integration 14. Scripting with the Exchange Web Services Managed API 15. Common Shell Information 16. Query Syntaxes

Setting up a PowerShell profile

You can use a PowerShell profile to customize your shell environment and to load functions, modules, aliases, and variables into the environment when you start your Exchange Management Shell session. In this recipe, we'll take a look at how you can create a profile.

How to do it...

Profiles are not created by default, but you may want to verify one has not already been created.

  1. Start off by running the Test-Path cmdlet:
    Test-Path $profile  
  1. If the Test-Path cmdlet returns $true, then a profile has already been created for the current user. You can open an existing profile by invoking notepad.exe from the shell:
    notepad $profile  
  1. If the Test-Path cmdlet returns $false, you can create a new profile for the current user by running the following command:
    New-Item -type file -path $profile -force  

How it works...

A PowerShell profile is a just a script with a .ps1 extension that is run every time you start the shell. You can think of a profile as a logon script for your PowerShell or Exchange Management Shell session. Inside your profile you can add custom aliases, define variables, load modules, or add your own functions so that they will be available every time you start the shell. In the previous example, we used the automatic shell $profile variable to create a profile script for the current user, which, in this case, would create the profile in the $env:UserProfile\Documents\WindowsPowerShell\directory.

Since PowerShell is simply executing a .ps1 script to load your profile, your execution policy must allow the execution of scripts on your machine. If it does not, your profile will not be loaded when starting the shell and you'll receive an error.

There are four types of profiles that can be used with PowerShell:

  • $Profile.AllUsersAllHosts: This profile applies to all users and all shells and is located in $env:Windir\system32\WindowsPowerShell\v1.0\profile.ps1
  • $Profile.AllUsersCurrentHost: This profile applies to all users but only the PowerShell.exe host and is located in $env:Windir\system32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
  • $Profile.CurrentUserAllHosts: This profile applies to the current user and all shells and is located in $env:UserProfile\Documents\WindowsPowerShell\profile.ps1
  • $Profile.CurrentUserCurrentHost: This profile applies to the current user and only to the PowerShell.exe host and is located in $env:UserProfile\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Using the $profile variable alone to create the profile will default to the CurrentUserCurrentHost location and is probably the most commonly-used profile type. If you need to create a profile for all the users on a machine, use one of the AllUsers profile types.

You may be wondering at this point what the difference is between the Current Host and All Hosts profile types. The PowerShell runtime can be hosted within third-party applications, so the All Hosts profile types apply to those instances of PowerShell. The Current Host profile types can be used with PowerShell.exe and when you are running the Exchange Management Shell.

In addition to defining custom aliases or functions in a profile, you may want to consider loading any other modules that may be useful. For example, you may want to load the Active Directory module for PowerShell so that those cmdlets are also available to you whenever you start the shell.

When you're done making changes to your profile, save and close the file. In order for the changes to take effect, you can either restart the shell, or you can dot-source the script to reload the profile:

    .$profile  

You can create multiple .ps1 scripts that include aliases, functions, and variables and then dot-source these scripts within your profile to have them loaded every time you start your PowerShell session.

To give an applicable example would be to use Import-Module inside the profile script to import PowerShell modules immediately when launching Windows PowerShell or Exchange Management Shell. Functions, modules, and other PS1 script files can also be loaded into the profile script.

You can reference the help system on this topic by running Get-Helpabout_profiles.

There's more...

Trying to remember all of the profile types and their associated script paths can be a little tough. There's actually a pretty neat trick that you can use with the $profile variable to view all of the profile types and file paths in the shell. To do this, access the psextended property of the $profile object:

    $profile.psextended | Format-List  

This will give you a list of each profile type and the path of the .ps1 script that should be used to create the profile.

See also

  • The Command aliases recipe in this chapter
  • The Working with variables and objects recipe in this chapter
You have been reading a chapter from
Microsoft Exchange Server 2016 PowerShell Cookbook - Fourth Edition
Published in: Jul 2017
Publisher:
ISBN-13: 9781787126930
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image