Reader small image

You're reading from  Learning PowerCLI - Second Edition

Product typeBook
Published inFeb 2017
Publisher
ISBN-139781786468017
Edition2nd Edition
Right arrow
Author (1)
Robert van den Nieuwendijk
Robert van den Nieuwendijk
author image
Robert van den Nieuwendijk

Robert van den Nieuwendijk is an IT veteran from the Netherlands with over thirty years of experience in Information Technology. He holds a bachelor degree in software engineering. After working a few years as a programmer of air traffic control and vessel traffic management systems, he started his own company Van den Nieuwendijk Informatica in 1988. Since then he has worked as a freelance systems administrator of OpenVMS, Windows Server, Linux, and VMware vSphere systems, for Dutch governmental organizations and cloud providers. During winter he is also a ski and snowboard instructor at an indoor ski school. With his background as a programmer, he always tries to make his job easier by writing programs or scripts to perform repeating tasks. In the past, he used the C programming language, OpenVMS DCL, Visual Basic Script and KiXtart to do this. Now, he uses Microsoft PowerShell and VMware PowerCLI for all of his scripting work. Robert is a frequent contributor and moderator at the VMware VMTN Communities. Since 2012 VMware awarded him the vExpert title for his significant contributions to the community and a willingness to share his expertise with others. He has a blog at http://rvdnieuwendijk.com where he writes mainly about VMware PowerCLI, Microsoft PowerShell, and VMware vSphere. If you want to get in touch with Robert, then you can find him on Twitter. His username is @rvdnieuwendijk. Robert is also the author of Learning PowerCLI, Packt Publishing.
Read more about Robert van den Nieuwendijk

Right arrow

Creating a PowerShell profile


If you want certain PowerCLI commands to be executed every time you start a PowerCLI session, you can put these commands in a PowerShell profile. The commands in a PowerShell profile will be executed every time you start a new PowerCLI session. There are six PowerShell profiles, two specific for the PowerShell console, two specific for the PowerShell Integrated Scripting Environment (ISE), and two used by both the PowerShell console and the PowerShell ISE. The PowerShell console and the PowerShell ISE have their own profiles for:

  • All users, current host

  • Current user, current host

The two profiles used by both the PowerShell console and the PowerShell ISE are:

  • All users, all hosts

  • Current user, all hosts

You can retrieve the locations for the different profiles of the PowerShell console by executing the following command in the PowerShell console. In this command, the $PROFILE variable is a standard PowerShell variable that returns an object containing the locations of the PowerShell profiles. This object is piped to the Format-List -Force command to display all of the properties of the $PROFILE object in a list:

PowerCLI C:\> $PROFILE | Format-List -Force


    AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\p
                         rofile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\M
                         icrosoft.PowerShell_profile.ps1
CurrentUserAllHosts    : C:\Users\robert\Documents\WindowsPowerShell\
                         profile.ps1
CurrentUserCurrentHost : C:\Users\robert\Documents\WindowsPowerShell\
                         Microsoft.PowerShell_profile.ps1
Length                 : 76

Note

Downloading the example code

Detailed steps to download the code bundle are mentioned in the Preface of this book. Please have a look.

The code bundle for the book is also hosted on GitHub at:  https://github.com/rosbook/effective_robotics_programming_with_ros . We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/ . Check them out!

As you can see in the output of the preceding command, the $PROFILE object has four properties AllUsersAllHosts, AllUsersCurrentHost, CurrentUserAllHosts, and CurrentUserCurrentHost that contain the locations of the different profiles.

To list the locations of the PowerShell profiles of the PowerShell ISE, you have to execute the preceding command in the PowerShell ISE. This gives the following output:

PS C:\> $PROFILE | Format-List -Force


    AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\p
                         rofile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\M
                         icrosoft.PowerShellISE_profile.ps1
CurrentUserAllHosts    : C:\Users\robert\Documents\WindowsPowerShell\
                         profile.ps1
CurrentUserCurrentHost : C:\Users\robert\Documents\WindowsPowerShell\
                         Microsoft.PowerShellISE_profile.ps1
Length                 : 79 

Note

You can start the PowerShell ISE from a Command Prompt by running powershell_ise.exe. You can start the PowerShell ISE from within a PowerShell console with the alias ise.

The default value for the $PROFILE variable is the value of the $PROFILE.CurrentUserCurrentHost property. So you can use $PROFILE instead of $PROFILE.CurrentUserCurrentHost.

You can determine if a specific profile exists by using the Test-Path cmdlet. The following command will test if the profile specified by $PROFILE exists:

PowerCLI C:\> Test-Path -Path $PROFILE
False

If a profile does not exist, as in the preceding example, you can create the profile using the New-Item cmdlet. If the directories in the path do not exist, by using the -Force parameter the New-Item cmdlet will create the directories. The following command will create the current user/current host profile and will also create the missing directories in the path:

PowerCLI C:\> New-Item -Path $PROFILE -ItemType file -Force

         Directory: C:\Users\robert\Documents\WindowsPowerShell

    Mode             LastWriteTime        Length  Name
----           ------------------     ------  ----
-a--           1/7/2017   2:01 PM          0  Microsoft.PowerShell_pro
                                              file.ps1

After creating the PowerShell profile, you can edit the profile using the PowerShell ISE with the following command:

PowerCLI C:\> ise $PROFILE

If you put the commands from the preceding section, Modifying the PowerShell execution policy, the new colors of the messages will be used in all of your PowerCLI sessions.

Previous PageNext Page
You have been reading a chapter from
Learning PowerCLI - Second Edition
Published in: Feb 2017Publisher: ISBN-13: 9781786468017
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 £13.99/month. Cancel anytime

Author (1)

author image
Robert van den Nieuwendijk

Robert van den Nieuwendijk is an IT veteran from the Netherlands with over thirty years of experience in Information Technology. He holds a bachelor degree in software engineering. After working a few years as a programmer of air traffic control and vessel traffic management systems, he started his own company Van den Nieuwendijk Informatica in 1988. Since then he has worked as a freelance systems administrator of OpenVMS, Windows Server, Linux, and VMware vSphere systems, for Dutch governmental organizations and cloud providers. During winter he is also a ski and snowboard instructor at an indoor ski school. With his background as a programmer, he always tries to make his job easier by writing programs or scripts to perform repeating tasks. In the past, he used the C programming language, OpenVMS DCL, Visual Basic Script and KiXtart to do this. Now, he uses Microsoft PowerShell and VMware PowerCLI for all of his scripting work. Robert is a frequent contributor and moderator at the VMware VMTN Communities. Since 2012 VMware awarded him the vExpert title for his significant contributions to the community and a willingness to share his expertise with others. He has a blog at http://rvdnieuwendijk.com where he writes mainly about VMware PowerCLI, Microsoft PowerShell, and VMware vSphere. If you want to get in touch with Robert, then you can find him on Twitter. His username is @rvdnieuwendijk. Robert is also the author of Learning PowerCLI, Packt Publishing.
Read more about Robert van den Nieuwendijk