diff --git a/reference/docs-conceptual/learn/ps101/00-introduction.md b/reference/docs-conceptual/learn/ps101/00-introduction.md index bc287dca0cf..f47e0b88f5b 100644 --- a/reference/docs-conceptual/learn/ps101/00-introduction.md +++ b/reference/docs-conceptual/learn/ps101/00-introduction.md @@ -1,7 +1,7 @@ --- -description: This is the introduction of the book PowerShell 101 by Mike F. Robbins. +description: Introduction of the book PowerShell 101 The No-Nonsense Guide to Windows PowerShell by Mike F. Robbins. ms.custom: Contributor-mikefrobbins -ms.date: 12/08/2022 +ms.date: 06/25/2024 ms.reviewer: mirobb title: Introduction --- @@ -10,51 +10,71 @@ title: Introduction
- PowerShell 101 (the book) + PowerShell 101 (the book) - This content originally appeared in the book PowerShell 101 by Mike F Robbins. We thank - Mike for granting us permission to reuse his content here. The content has been edited from the - original publication. You can still get the original book from Leanpub at + This content originally appeared in the book PowerShell 101 by Mike F. Robbins. We thank + Mike for granting us permission to reuse the content here. We edited the content for publication + on this platform. You can still get the original book from Leanpub at PowerShell 101.
-## Who is this book for? +## About this book -This is an entry-level book for anyone wanting to learn PowerShell. +Before PowerShell, I began my career as an IT Pro, pointing and clicking in the GUI. I wrote this +book to save IT Pros from themselves by reducing the learning curve and helping them avoid being +reluctant to learn PowerShell. -This book focuses on PowerShell version 5.1 running on Windows 10 and Windows Server 2016 in a -Microsoft Active Directory domain environment. However, the basic concepts apply to all versions of -PowerShell running on any supported platform. +Instead of a book that covers topics with fictitious scenarios, this book is a condensed version +targeting the specific topics that an IT Pro needs to know to succeed with PowerShell in a +real-world production environment. It's a collection of what I wish someone had told me when I +started learning PowerShell. I include tips, tricks, and best practices that I learned while using +PowerShell since 2007. -## About this book +Each chapter includes a curated collection of links to specific help articles that expand on the +information covered. These resources expand on the concepts discussed and broaden your understanding +of PowerShell. + +## Who is this book for? -This book is a collection of what I wish someone would have told me when I started learning -PowerShell, along with the tips, tricks, and best practices that I've learned while using PowerShell -during the past 10 years. +This book is for anyone wanting to learn PowerShell. Whether you're a beginner or an experienced +user, this book helps you improve your PowerShell skills. -Instead of providing an enormous amount of information, this book attempts to provide a balance of -enough information to be successful for someone who is just getting started with PowerShell. Each -chapter contains links to specific help topics for those who want more information about the topics -covered in that chapter. +This book focuses on Windows PowerShell version 5.1 running on Windows 11 and Windows Server 2022 in +a Microsoft Active Directory domain environment. However, the basic concepts apply to all versions +of PowerShell running on any supported platform. + +## Lab environment + +The examples in this book were created and tested on Windows 11 and Windows Server 2022 operating +systems, using Windows PowerShell version 5.1. If you're running a different operating system or +version of PowerShell, your results might vary from the ones presented in this book. ## About the author -Mike F Robbins is a former Microsoft MVP, co-author of _Windows PowerShell TFM 4th Edition_, and a -contributing author in the _PowerShell Deep Dives_ book. Mike has been a strong supporter of the -PowerShell community and is now the lead writer for [Azure PowerShell][Azure PowerShell] at Microsoft. He blogs at -[mikefrobbins.com][mikefrobbins.com] and can be found on twitter [@mikefrobbins][@mikefrobbins]. +Mike F. Robbins, a former Microsoft MVP, is the lead technical writer for +[Azure PowerShell][azps-docs] at Microsoft. With extensive experience in PowerShell, he is a +scripting, automation, and efficiency expert. As a lifelong learner, Mike continuously strives to +improve his skills and empower others by sharing his knowledge and experience. He is also a +published author of several books, including: -## Lab environment +- Author of [PowerShell 101: The No-Nonsense Guide to Windows PowerShell][powershell-101] +- Creator of [The PowerShell Conference Book][psconf-book] +- Coauthor of [Windows PowerShell TFM 4th Edition][tfm4] +- Contributing author in the [PowerShell Deep Dives][ps-deep-dives] book -The examples in this book were designed and tested on Windows 10 Anniversary Edition (build 1607) -and Windows Server 2016 using PowerShell version 5.1. If you're using a different version of -PowerShell or operating system, your results may differ from those shown here. +When Mike's not writing documentation for Microsoft, he shares his thoughts and insights on his blog +at [mikefrobbins.com][mikefrobbins-com] and interacts with his followers on Twitter +[@mikefrobbins][mikefrobbins-x]. -[@mikefrobbins]: https://twitter.com/mikefrobbins -[mikefrobbins.com]: https://mikefrobbins.com/ -[PowerShell 101]: https://leanpub.com/powershell101 -[Azure PowerShell]: /powershell/azure + +[azps-docs]: /powershell/azure +[powershell-101]: https://leanpub.com/powershell101 +[psconf-book]: https://leanpub.com/powershell-conference-book +[tfm4]: https://www.sapien.com/books_training/Windows-PowerShell-4 +[ps-deep-dives]: https://www.manning.com/books/powershell-deep-dives +[mikefrobbins-com]: https://mikefrobbins.com/ +[mikefrobbins-x]: https://twitter.com/mikefrobbins diff --git a/reference/docs-conceptual/learn/ps101/01-getting-started.md b/reference/docs-conceptual/learn/ps101/01-getting-started.md index 273aa92958b..42b18d5249f 100644 --- a/reference/docs-conceptual/learn/ps101/01-getting-started.md +++ b/reference/docs-conceptual/learn/ps101/01-getting-started.md @@ -1,132 +1,150 @@ --- description: Where to find and how to launch PowerShell for new users. ms.custom: Contributor-mikefrobbins -ms.date: 11/16/2022 +ms.date: 06/25/2024 ms.reviewer: mirobb -title: Getting Started with PowerShell +title: Getting started with PowerShell --- -# Chapter 1 - Getting Started with PowerShell -I often find that presenters at conferences and user group meetings already have PowerShell running -when they start entry-level presentations. This book begins by answering the questions I've heard -attendees who haven't previously used PowerShell ask in those sessions. +# Chapter 1 - Getting started with PowerShell -Specifically, this chapter focuses on finding and launching PowerShell, and solving some of the -initial pain points that new users experience with PowerShell. Be sure to follow along and -walk through the examples shown in this chapter on your Windows 10 lab environment computer. +This chapter focuses on finding and launching PowerShell and solving the initial pain points that +new users experience with PowerShell. Follow along and walk through the examples in this chapter on +your lab environment computer. -## What do I need to get started with PowerShell? +## What is PowerShell? -All modern versions of Windows operating systems ship with PowerShell installed. If you're running a version older than 5.1, you should install the latest version. +Windows PowerShell is an easy-to-use command-line shell and scripting environment for automating +administrative tasks of Windows-based systems. Windows PowerShell is preinstalled on all modern +versions of the Windows operating system. -- To upgrade to Windows PowerShell 5.1, see [Upgrading existing Windows PowerShell][Upgrading existing Windows PowerShell] -- To install the latest version of PowerShell, see [Installing PowerShell][Installing PowerShell] +## Where to find PowerShell -## Where do I find PowerShell? +The easiest way to find PowerShell on Windows 11 is to type `PowerShell` into the search bar, as +shown in Figure 1-1. Notice that there are four different shortcuts for Windows PowerShell. -The easiest way to find PowerShell on Windows 10 is to type **PowerShell** into the search bar as -shown in Figure 1-1. +![Figure 1-1 - Search for PowerShell.](media/figure1-1.jpg) -![Figure 1-1 - Search for PowerShell in the Start Menu](media/figure1-1.png) +Windows PowerShell shortcuts on a 64-bit version of Windows: -Notice that four different shortcuts for PowerShell are shown in Figure 1-1. The computer used for -demonstration purposes in this book is running the 64-bit version of Windows 10 so there's a 64-bit -version of the PowerShell console and the PowerShell ISE (Integrated Scripting Environment), and a -32-bit version of each one as denoted by the (x86) suffix on the shortcuts. If you happen to be -running a 32-bit version of Windows 10, you'll only have two shortcuts. Those items don't have the -(x86) suffix, but are 32-bit versions. If you have a 64-bit operating system, my recommendation is -to run the 64-bit version of PowerShell unless you have a specific reason for running the 32-bit -version. +- Windows PowerShell +- Windows PowerShell ISE +- Windows PowerShell (x86) +- Windows PowerShell ISE (x86) -For information about starting PowerShell on other versions of Windows, see -[Starting Windows PowerShell][Starting Windows PowerShell]. +On a 64-bit version of Windows, you have a 64-bit version of the Windows PowerShell console and the +Windows PowerShell Integrated Scripting Environment (ISE) and a 32-bit version of each one, as +indicated by the (x86) suffix on the shortcuts. -## How do I launch PowerShell? +> [!NOTE] +> Windows 11 only ships as a 64-bit operating system. There is no 32-bit version of Windows 11. +> However, Windows 11 include 32-bit versions of Windows PowerShell and the Windows PowerShell ISE. -In the production enterprise environments that I support, I use three different Active Directory -user accounts. I've mirrored those accounts in the lab environment used in this book. I log into the -Windows 10 computer as a domain user who is not a domain or local administrator. +You only have two shortcuts if you're running an older 32-bit version of Windows. Those shortcuts +don't have the (x86) suffix but are 32-bit versions. -I've launched the PowerShell console by clicking on the "Windows PowerShell" shortcut as shown in -Figure 1-1. +I recommend using the 64-bit version of Windows PowerShell if you're running a 64-bit operating +system unless you have a specific reason for using the 32-bit version. -![Figure 1-4 - Title bar of the PowerShell window](media/figure1-4.png) +Depending on what version of Windows 11 you're running, Windows PowerShell might open in +[Windows Terminal][windows-terminal]. -Notice that the title bar of the PowerShell console says "Windows PowerShell" as shown in Figure -1-4. Some commands run fine, but PowerShell can't participate in User Access Control (UAC). That -means it's unable to prompt for elevation for tasks that require the approval of an administrator. -The following error message is generated: +Microsoft no longer updates the PowerShell ISE. The ISE only works with Windows PowerShell 5.1. +[Visual Studio Code][vs-code] (VS Code) with the [PowerShell extension][ps-ext] works with both +versions of PowerShell. VS Code and the PowerShell extension don't ship in Windows. Install VS Code +and the extension on the computer where you create PowerShell scripts. You don't need to install +them on all the computers where you run PowerShell. + +## How to launch PowerShell + +I use three different Active Directory user accounts in the production environments I support. I +mirrored those accounts in the lab environment used in this book. I sign into my Windows 11 computer +as a domain user without domain or local administrator rights. + +Launch the PowerShell console by clicking the **Windows PowerShell** shortcut, as shown in Figure +1-1. Notice that the title bar of the console says **Windows PowerShell**, as shown in Figure 1-2. + +![Figure 1-2 - Title bar of PowerShell window.](media/figure1-2.jpg) + +Some commands run fine when you run PowerShell as an ordinary user. However, PowerShell doesn't +participate in [User Access Control (UAC)][uac]. That means it's unable to prompt for elevation for +tasks that require the approval of an administrator. + +> [!NOTE] +> UAC is a Windows security feature that helps prevent malicious code from running with elevated +> privileges. + +When signed on as an ordinary user, PowerShell returns an error when you run a command that requires +elevation. For example, stopping a Windows service: ```powershell -Get-Service -Name W32Time | Stop-Service +Stop-Service -Name W32Time ``` ```Output -Stop-Service : Service 'Windows Time (W32Time)' cannot be stopped due to the following -error: Cannot open W32Time service on computer '.'. -At line:1 char:29 -+ Get-Service -Name W32Time | Stop-Service -+ - + CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) - [Stop-Service], ServiceCommandException - + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.StopServiceCommand +Stop-Service : Service 'Windows Time (W32Time)' cannot be stopped due to +the following error: Cannot open W32Time service on computer '.'. +At line:1 char:1 ++ Stop-Service -Name W32Time ++ ~~~~~~~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo : CloseError: (System.ServiceProcess.ServiceCon + troller:ServiceController) [Stop-Service], ServiceCommandException + + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Comm + ands.StopServiceCommand ``` -The solution to this problem is to run PowerShell as a domain user who is a local administrator. -This is how my second domain user account is configured. Using the principle of least privilege, -this account should NOT be a domain administrator, or have any elevated privileges in the domain. - -Close PowerShell. Relaunch the PowerShell console, except this time right-click on the **Windows -PowerShell** shortcut and select **Run as administrator** as shown in Figure 1-5. +The solution is to run PowerShell elevated as a user who is a local administrator. That's how I +configured my second domain user account. Following the principle of least privilege, this account +shouldn't be a domain administrator or have any elevated privileges in the domain. -![Figure 1-5 - Context menu - Run as administrator](media/figure1-5.png) +To start PowerShell with elevated rights, right-click the **Windows PowerShell** shortcut and select +**Run as administrator**, as shown in Figure 1-3. -If you're logged into Windows as a normal user, you'll be prompted for credentials. I'll enter the -credentials for my user account who is a domain user and local admin as shown in Figure 1-6. +![Figure 1-3 - Context menu - Run as administrator.](media/figure1-3.jpg) -![Figure 1-6](media/figure1-6.png) +Windows prompts you for credentials because you logged into Windows as an ordinary user. Enter the +credentials of your domain user who is a local administrator, as shown in Figure 1-4. -Once PowerShell is relaunched as an administrator, the title bar should say "Administrator: Windows -PowerShell" as shown in Figure 1-7. +![Figure 1-4 - User account control - Enter credentials.](media/figure1-4.jpg) -![Figure 1-7](media/figure1-7.png) +Notice that the title bar of the elevated console windows says **Administrator: Windows +PowerShell**, as shown in Figure 1-5. -Now that PowerShell is being run elevated as a local administrator, UAC will no longer be a problem -when a command is run on the local computer that would normally require a prompt for elevation. Keep -in mind though that any command run from this elevated instance of the PowerShell console, also runs -elevated. +![Figure 1-5 - Title bar of elevated PowerShell window.](media/figure1-5.jpg) -To simplify finding PowerShell and launching it as an administrator, I recommend pinning it to the -taskbar and setting it to automatically launch as an admin each time it's run. +Now that you're running PowerShell elevated as an administrator, UAC is no longer a problem when you +run a command that requires elevation. -Search for PowerShell again, except this time right-click on it and select "Pin to taskbar" as shown -in Figure 1-8. +> [!IMPORTANT] +> You should only run PowerShell elevated as an administrator when absolutely necessary. -![Figure 1-8](media/figure1-8.png) +When you target remote computers, there's no need to run PowerShell elevated. Running PowerShell +elevated only affects commands that run against your local computer. -Right-click on the PowerShell shortcut that's now pinned to the taskbar and select properties as -shown in Figure 1-9. +You can simplify finding and launching PowerShell. Pin the PowerShell or Windows Terminal shortcut +to your taskbar. Search for PowerShell again, except this time right-click on it and select **Pin to +taskbar** as shown in Figure 1-6. -![Figure 1-9 - User account control - enter credentials](media/figure1-9.png) +![Figure 1-6 - Context menu - Pin to taskbar.](media/figure1-6.jpg) -Click on "Advanced" as denoted by #1 in Figure 1-10, then check the "Run as administrator" checkbox -as denoted by #2 in Figure 1-10, and then click OK twice to accept the changes and exit out of both -dialog boxes. +> [!IMPORTANT] +> The original version of this book, published in 2017, recommended pinning a shortcut to the +> taskbar to launch an elevated instance automatically every time you start PowerShell. However, due +> to potential security concerns, I no longer recommended it. Any application you launch from an +> elevated instance of PowerShell also bypass UAC and run elevated. For example, if you launch a web +> browser from an elevated instance of PowerShell, any website you visit containing malicious code +> also runs elevated. -![Figure 1-10 - Title bar showing "Administrator"](media/figure1-10.png) +When you need to run PowerShell with elevated permissions, right-click the PowerShell shortcut +pinned to your taskbar while pressing Shift. Select **Run as administrator**, as shown in +Figure 1-7. -You'll never have to worry about finding PowerShell or whether or not it's running as an -administrator again. +![Figure 1-7 - Context menu - Run as administrator.](media/figure1-7.jpg) -Running PowerShell elevated as an administrator to prevent having problems with UAC only impacts -commands that are run against the local computer. It has no effect on commands that target remote -computers. +## Determine your version of PowerShell -## What version of PowerShell am I running? - -There are a number of automatic variables in PowerShell that store state information. One of these -variables is `$PSVersionTable`, which contains a hashtable that can be used to display the relevant -PowerShell version information: +There are automatic variables in PowerShell that store state information. One of these variables is +`$PSVersionTable`, which contains version information about your PowerShell session. ```powershell $PSVersionTable @@ -135,40 +153,53 @@ $PSVersionTable ```Output Name Value ---- ----- -PSVersion 5.1.19041.1 +PSVersion 5.1.22621.2428 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} -BuildVersion 10.0.19041.1 +BuildVersion 10.0.22621.2428 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 ``` -Newer versions of Windows PowerShell are distributed as part of the Windows Management Framework -(WMF). A specific version of the .NET Framework is required depending on the WMF version. To upgrade -to Windows PowerShell 5.1, see [Upgrading existing Windows PowerShell][Upgrading existing Windows PowerShell]. +If you're running a version of Windows PowerShell older than 5.1, you should update your version of +Windows. Windows PowerShell 5.1 is preinstalled on the currently supported versions of Windows. -## Execution Policy +PowerShell version 7 isn't a replacement for Windows PowerShell 5.1; it installs side-by-side with +Windows PowerShell. Windows PowerShell version 5.1 and PowerShell version 7 are two different +products. For more information about the differences between Windows PowerShell version 5.1 and +PowerShell version 7, see [Migrating from Windows PowerShell 5.1 to PowerShell 7][migrate-ps]. -Contrary to popular belief, the execution policy in PowerShell is not a security boundary. It's -designed to prevent a user from unknowingly running a script. A determined user can easily bypass -the execution policy in PowerShell. Table 1-2 shows the default execution policy for current -Windows operating systems. +> [!TIP] +> PowerShell version 6, formerly known as PowerShell Core, is no longer supported. + +## Execution policy + +PowerShell execution policy controls the conditions under which you can run PowerShell scripts. The +execution policy in PowerShell is a safety feature designed to help prevent the unintentional +execution of malicious scripts. However, it's not a security boundary because it can't stop +determined users from deliberately running scripts. A determined user can bypass the execution +policy in PowerShell. + +You can set an execution policy for the local computer, current user, or a PowerShell session. You +can also set execution policies for users and computers with Group Policy. + +The following table shows the default execution policy for current Windows operating systems. | Windows Operating System Version | Default Execution Policy | | -------------------------------- | ------------------------ | -| Server 2019 | Remote Signed | -| Server 2016 | Remote Signed | +| Windows Server 2022 | Remote Signed | +| Windows Server 2019 | Remote Signed | +| Windows Server 2016 | Remote Signed | +| Windows 11 | Restricted | | Windows 10 | Restricted | -Regardless of the execution policy setting, any PowerShell command can be run interactively. The -execution policy only affects commands running in a script. The `Get-ExecutionPolicy` cmdlet is used -to determine what the current execution policy setting is and the `Set-ExecutionPolicy` cmdlet is -used to change the execution policy. My recommendation is to use the **RemoteSigned** policy, which -requires downloaded scripts to be signed by a trusted publisher in order to be run. +Regardless of the execution policy setting, you can run any PowerShell command interactively. The +execution policy only affects commands running in a script. Use the `Get-ExecutionPolicy` cmdlet to +determine the current execution policy setting. -Check the current execution policy: +Check the execution policy setting on your computer. ```powershell Get-ExecutionPolicy @@ -178,113 +209,160 @@ Get-ExecutionPolicy Restricted ``` -PowerShell scripts can't be run at all when the execution policy is set to **Restricted**. This is -the default setting on all Windows client operating systems. To demonstrate the problem, save the -following code as a `.ps1` file named `Stop-TimeService.ps1`. +List the execution policy settings for all scopes. + +```powershell +Get-ExecutionPolicy -List +``` + +```Output + Scope ExecutionPolicy + ----- --------------- +MachinePolicy Undefined + UserPolicy Undefined + Process Undefined + CurrentUser Undefined + LocalMachine Undefined +``` + +All Windows client operating systems have the default execution policy setting of `Restricted`. You +can't run PowerShell scripts using the `Restricted` execution policy setting. To test the execution +policy, save the following code as a `.ps1` file named `Get-TimeService.ps1`. > [!TIP] -> A PowerShell script is a plaintext file with a `.ps1` extension that contains the commands you -> want to run. To create a PowerShell script, use a code editor like Visual Studio Code (VS Code) or -> any text editor such as Notepad. +> A PowerShell script is a plaintext file that contains the commands you want to run. PowerShell +> script files use the `.ps1` file extension. To create a PowerShell script, use a code editor like +> Visual Studio Code (VS Code) or any text editor such as Notepad. + +When you run the following command interactively, it completes without error. ```powershell -Get-Service -Name W32Time | Stop-Service -PassThru +Get-Service -Name W32Time ``` -That command runs interactively without error as long as PowerShell is run elevated as an -administrator. But as soon as it's saved as a script file and you try to execute the script, it -generates an error: +However, PowerShell returns an error when you run the same command from a script. ```powershell -.\Stop-TimeService.ps1 +.\Get-TimeService.ps1 ``` ```Output -.\Stop-TimeService.ps1 : File C:\demo\Stop-TimeService.ps1 cannot be loaded because -running scripts is disabled on this system. For more information, see -about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170. +.\Get-TimeService.ps1 : File C:\tmp\Get-TimeService.ps1 cannot be loaded +because running scripts is disabled on this system. For more information, +see about_Execution_Policies at +https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 -+ .\Stop-TimeService.ps1 -+ ++ .\Get-TimeService.ps1 ++ ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess ``` -Notice that the error shown in the previous set of results tells you exactly what the problem is -(running scripts is disabled on this system). When you run a command in PowerShell that generates an -error message, be sure to read the error message instead of just rerunning the command and hoping -that it runs successfully. +When you run a command in PowerShell that generates an error, read the error message before retrying +the command. Notice the error message tells you why the command failed: + +> _... running scripts is disabled on this system._ + +To enable the execution of scripts, change the execution policy with the `Set-ExecutionPolicy` +cmdlet. `LocalMachine` is the default scope when you don't specify the **Scope** parameter. You must +run PowerShell elevated as an administrator to change the execution policy for the local machine. If +you're signing your scripts, I recommend using the `RemoteSigned` execution policy. `RemoteSigned` +prevents you from running downloaded scripts that aren't signed by a trusted publisher. + +Before you change the execution policy, read the [about_Execution_Policies][execution-policies] help +article to understand the security implications. -Change the PowerShell execution policy to remote signed. +Change the execution policy setting on your computer to `RemoteSigned`. ```powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned ``` +If you have successfully changed the execution policy, PowerShell displays the following warning: + ```Output Execution Policy Change -The execution policy helps protect you from scripts that you do not trust. Changing the execution -policy might expose you to the security risks described in the about_Execution_Policies help topic -at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy? -[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):y +The execution policy helps protect you from scripts that you do not trust. +Changing the execution policy might expose you to the security risks +described in the about_Execution_Policies help topic at +https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the +execution policy? +[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help +(default is "N"):y +``` + +If you're not running PowerShell elevated as an administrator, PowerShell returns the following +error message: + +```Output +Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWAR +E\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To +change the execution policy for the default (LocalMachine) scope, start +Windows PowerShell with the "Run as administrator" option. To change the +execution policy for the current user, run "Set-ExecutionPolicy -Scope +CurrentUser". +At line:1 char:1 ++ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned ++ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], + UnauthorizedAccessException + + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft. + PowerShell.Commands.SetExecutionPolicyCommand ``` -Be sure to read the warning that's displayed when changing the execution policy. I also recommend -taking a look at the [about_Execution_Policies][about_Execution_Policies] help topic to make sure you understand the -security implications of changing the execution policy. +It's also possible to change the execution policy for the current user without requiring you to run +PowerShell elevated as an administrator. This step is unnecessary if you successfully set the +execution policy for the local machine to `RemoteSigned`. -Now that the execution policy has been set to **RemoteSigned**, the `Stop-TimeService.ps1` script -runs error free. +```powershell +Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser +``` + +With the execution policy set to `RemoteSigned`, the `Get-TimeService.ps1` script runs successfully. ```powershell -.\Stop-TimeService.ps1 +.\Get-TimeService.ps1 ``` ```Output Status Name DisplayName ------ ---- ----------- -Stopped W32Time Windows Time -``` - -Be sure to start your Windows Time service before continuing otherwise you may run into unforeseen -problems. - -```powershell -Start-Service -Name w32time +Running W32Time Windows Time ``` ## Summary -In this chapter, you've learned how to find and launch PowerShell, and how to create a shortcut that -launches PowerShell as an administrator. You've also learned about the default execution policy and -how to change it. +In this chapter, you learned where to find and how to launch PowerShell. You also learned how to +determine the version of PowerShell and the purpose of execution policies. ## Review 1. How do you determine what PowerShell version a computer is running? -1. Why is it important to launch PowerShell elevated as an administrator? -1. How do you determine the current PowerShell execution policy? -1. What does the default PowerShell execution policy on Windows client computers prevent from - occurring? +1. When should you launch PowerShell elevated as an administrator? +1. What's the default execution policy on Windows client computers, and what does it prevent you + from doing? +1. How do you determine the current PowerShell execution policy setting? 1. How do you change the PowerShell execution policy? -## Recommended Reading +## References -For those who want to know more information about the topics covered in this chapter, I recommend -reading the following PowerShell help topics. +To learn more about the concepts covered in this chapter, read the following PowerShell help +articles. -- [about_Automatic_Variables][about_Automatic_Variables] -- [about_Hash_Tables][about_Hash_Tables] -- [about_Execution_Policies][about_Execution_Policies] +- [about_Automatic_Variables][auto-variables] +- [about_Execution_Policies][execution-policies] -In the next chapter, you'll learn about the discoverability of commands in PowerShell. One of the -things that will be covered is how to update PowerShell so those help topics can be viewed right -from within PowerShell instead of having to view them on the internet. +## Next steps + +In the next chapter, you'll learn about the discoverability of commands in PowerShell. You'll also +learn how to download PowerShell's help files so you can view the help in your PowerShell session. -[about_Automatic_Variables]: /powershell/module/microsoft.powershell.core/about/about_automatic_variables -[about_Hash_Tables]: /powershell/module/microsoft.powershell.core/about/about_hash_tables -[about_Execution_Policies]: /powershell/module/microsoft.powershell.core/about/about_execution_policies -[Upgrading existing Windows PowerShell]: /powershell/scripting/windows-powershell/install/installing-windows-powershell#upgrading-existing-windows-powershell -[Installing PowerShell]: /powershell/scripting/install/installing-powershell -[Starting Windows PowerShell]: /powershell/scripting/windows-powershell/starting-windows-powershell + +[windows-terminal]: /windows/terminal/ +[vs-code]: https://code.visualstudio.com/ +[ps-ext]: https://code.visualstudio.com/docs/languages/powershell +[uac]: /windows/security/application-security/application-control/user-account-control/ +[migrate-ps]: /powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7 +[execution-policies]: /powershell/module/microsoft.powershell.core/about/about_execution_policies +[auto-variables]: /powershell/module/microsoft.powershell.core/about/about_automatic_variables diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-1.jpg b/reference/docs-conceptual/learn/ps101/media/figure1-1.jpg new file mode 100644 index 00000000000..e371157b2b3 Binary files /dev/null and b/reference/docs-conceptual/learn/ps101/media/figure1-1.jpg differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-1.png b/reference/docs-conceptual/learn/ps101/media/figure1-1.png deleted file mode 100644 index 48ad183c491..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-1.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-10.png b/reference/docs-conceptual/learn/ps101/media/figure1-10.png deleted file mode 100644 index 5d151105f6d..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-10.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-11.png b/reference/docs-conceptual/learn/ps101/media/figure1-11.png deleted file mode 100644 index 6e659f9e722..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-11.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-2.jpg b/reference/docs-conceptual/learn/ps101/media/figure1-2.jpg new file mode 100644 index 00000000000..c2adb5e331e Binary files /dev/null and b/reference/docs-conceptual/learn/ps101/media/figure1-2.jpg differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-3.jpg b/reference/docs-conceptual/learn/ps101/media/figure1-3.jpg new file mode 100644 index 00000000000..5d6b3288511 Binary files /dev/null and b/reference/docs-conceptual/learn/ps101/media/figure1-3.jpg differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-4.jpg b/reference/docs-conceptual/learn/ps101/media/figure1-4.jpg new file mode 100644 index 00000000000..29f8329e403 Binary files /dev/null and b/reference/docs-conceptual/learn/ps101/media/figure1-4.jpg differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-4.png b/reference/docs-conceptual/learn/ps101/media/figure1-4.png deleted file mode 100644 index 6ad3b848ab2..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-4.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-5.jpg b/reference/docs-conceptual/learn/ps101/media/figure1-5.jpg new file mode 100644 index 00000000000..3640d4fd3c5 Binary files /dev/null and b/reference/docs-conceptual/learn/ps101/media/figure1-5.jpg differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-5.png b/reference/docs-conceptual/learn/ps101/media/figure1-5.png deleted file mode 100644 index e68bfb9002e..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-5.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-6.jpg b/reference/docs-conceptual/learn/ps101/media/figure1-6.jpg new file mode 100644 index 00000000000..855755730ac Binary files /dev/null and b/reference/docs-conceptual/learn/ps101/media/figure1-6.jpg differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-6.png b/reference/docs-conceptual/learn/ps101/media/figure1-6.png deleted file mode 100644 index 1b2aeb7f7fd..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-6.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-7.jpg b/reference/docs-conceptual/learn/ps101/media/figure1-7.jpg new file mode 100644 index 00000000000..d7ec15caa0f Binary files /dev/null and b/reference/docs-conceptual/learn/ps101/media/figure1-7.jpg differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-7.png b/reference/docs-conceptual/learn/ps101/media/figure1-7.png deleted file mode 100644 index 24b60a6525e..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-7.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-8.png b/reference/docs-conceptual/learn/ps101/media/figure1-8.png deleted file mode 100644 index 17c2004fccc..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-8.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/figure1-9.png b/reference/docs-conceptual/learn/ps101/media/figure1-9.png deleted file mode 100644 index 6e388eac53e..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/figure1-9.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/powershell101-150x194.png b/reference/docs-conceptual/learn/ps101/media/powershell101-150x194.png deleted file mode 100644 index 640a0b0c725..00000000000 Binary files a/reference/docs-conceptual/learn/ps101/media/powershell101-150x194.png and /dev/null differ diff --git a/reference/docs-conceptual/learn/ps101/media/powershell101-final-new-210x273.png b/reference/docs-conceptual/learn/ps101/media/powershell101-final-new-210x273.png new file mode 100644 index 00000000000..83788a08cad Binary files /dev/null and b/reference/docs-conceptual/learn/ps101/media/powershell101-final-new-210x273.png differ diff --git a/reference/docs-conceptual/toc.yml b/reference/docs-conceptual/toc.yml index b109bade4f2..7c2673c02aa 100644 --- a/reference/docs-conceptual/toc.yml +++ b/reference/docs-conceptual/toc.yml @@ -48,7 +48,7 @@ items: items: - name: Introduction href: learn/ps101/00-introduction.md - - name: Getting Started with PowerShell + - name: Getting started with PowerShell href: learn/ps101/01-getting-started.md - name: The Help System href: learn/ps101/02-help-system.md