Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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
Windows Server Automation with PowerShell Cookbook, Fifth Edition

You're reading from   Windows Server Automation with PowerShell Cookbook, Fifth Edition Powerful ways to automate, manage, and administrate Windows Server 2022 using PowerShell 7.2

Arrow left icon
Product type Paperback
Published in Jan 2023
Publisher Packt
ISBN-13 9781804614235
Length 714 pages
Edition 5th Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Thomas Lee Thomas Lee
Author Profile Icon Thomas Lee
Thomas Lee
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Installing and Configuring PowerShell 7 FREE CHAPTER 2. Managing PowerShell 7 in the Enterprise 3. Exploring .NET 4. Managing Active Directory 5. Managing Networking 6. Implementing Enterprise Security 7. Managing Storage 8. Managing Shared Data 9. Managing Printing 10. Exploring Windows Containers 11. Managing Hyper-V 12. Debugging and Troubleshooting Windows Server 13. Managing Windows Server with Window Management Instrumentation (WMI) 14. Managing Windows Update Services 15. Other Books You May Enjoy
16. Index

Building PowerShell 7 Profile Files

Profile files are PowerShell scripts that PowerShell runs at startup. They are easy to create and support a range of deployment scenarios. They enable you to customize your PowerShell environment. See this article on Microsoft’s PowerShell Community blog for more details on PowerShell profile files: https://devblogs.microsoft.com/powershell-community/how-to-make-use-of-powershell-profile-files/.

In this recipe, you examine profile files, download a sample PowerShell profile file, and install it on SRV1. This profile is just for the console. In a later recipe, you install VS Code and create a VS Code-specific profile.

Getting ready

You run this recipe on SRV1 after you have installed PowerShell 7. You should begin this recipe by opening up a PowerShell 7 console.

How to do it...

  1. Discovering the profile filenames
    $ProfileFiles = $PROFILE |  Get-Member -MemberType NoteProperty
    $ProfileFiles | Format-Table -Property Name, Definition
    
  2. Checking for the existence of each PowerShell profile file
    Foreach ($ProfileFile in $ProfileFiles){
      "Testing $($ProfileFile.Name)"
      $ProfilePath = $ProfileFile.Definition.split('=')[1]
      If (Test-Path -Path $ProfilePath){
        "$($ProfileFile.Name) DOES EXIST"
        "At $ProfilePath"
      }
      Else {
        "$($ProfileFile.Name) DOES NOT EXIST"
      }
      ""
    }
    
  3. Discovering a Current User/Current Host profile
    $CUCHProfile = $PROFILE.CurrentUserCurrentHost
    "Current User/Current Host profile path: [$CUCHPROFILE]"
    
  4. Creating a Current User/Current Host profile for the PowerShell 7 console
    $URI = 'https://raw.githubusercontent.com/doctordns/PacktPS72/master/' +
           'scripts/goodies/Microsoft.PowerShell_Profile.ps1'
    New-Item $CUCHProfile -Force -WarningAction SilentlyContinue |
       Out-Null
    (Invoke-WebRequest -Uri $URI).Content |
      Out-File -FilePath  $CUCHProfile
    
  5. Exiting from the PowerShell 7 console
    Exit
    
  6. Restarting the PowerShell 7 console and viewing the profile output at startup
    Get-ChildItem -Path $PROFILE
    

How it works...

In step 1, you use the $Profile built-in variable to obtain the filenames of the four profile files in PowerShell, with output like this:

Figure 1.19: Obtaining the PowerShell profile filenames

In step 2, you check to see which, if any, of the four profiles exist, with output like this:

Figure 1.20: Checking for the existence of the profile files

The profile file most IT pros use is the Current User/Current Host profile (aka $Profile). In step 3, you discover the filename for this profile file, with the following output:

Figure 1.21: Viewing the name of the Current User/Current Host profile file

In step 4, you download a sample PowerShell console profile file from GitHub. This step creates no output. After making a new profile file, in step 5, you exit PowerShell 7. After restarting the console, in step 6, you view the details of this profile file. The output from this step looks like this:

Figure 1.22: Viewing the name of the Current User/Current Host profile file

There’s more...

In step 1, you view the built-in profile filenames. As you can see, PowerShell has four profile files you can use. These files enable you to configure a given PowerShell host or all hosts for one or all users. As you can see in step 2, none of the four profile files exist by default.

In step 4, you create the Current User/Current Host profile file based on a code sample you download from GitHub. This profile file is a starting point and demonstrates many things you can do in a profile file.

In step 6, you view the profile file you created earlier. Also, notice that the prompt has changed – the current working directory when you start PowerShell is now C:\Foo.

You have been reading a chapter from
Windows Server Automation with PowerShell Cookbook, Fifth Edition - Fifth Edition
Published in: Jan 2023
Publisher: Packt
ISBN-13: 9781804614235
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