In an effort to become familiar with the look and feel of Windows Server 2016, you will learn how to navigate through some daily tasks using the graphical interface. On our agenda in this chapter are the following recipes:
- Shutting down or restarting the server
- Launching Administrative Tools
- Using WinKey + X for quick admin tasks
- Using the search function to launch applications quickly
- Managing remote servers from a single pane with Server Manager
- Using PowerShell to accomplish any function in Windows Server
- Installing a role or feature
- Administering Server 2016 from a Windows 10 machine
- Identifying useful keyboard shortcuts in Server 2016
- Setting your PowerShell Execution Policy
- Building and executing your first PowerShell script
- Searching for PowerShell cmdlets with Get-Help
Windows 8 and Server 2012 brought us a drastic change in the way that we interfaced with the Windows operating system, and most of us didn't think that change was for the better. By now I assume you have all seen, used, and are hopefully deploying Windows 10 on your client computers, which brings some relief with regard to the user interface. With Windows 10 we have kind of a mix between Windows 7 and Windows 8, and it fits the needs of most people in a better way. Just like the last couple of rollouts of the Microsoft Windows operating systems, the Server platform follows on the heels of the Desktop version, and the look and feel of Windows Server 2016 is very much like Windows 10. In fact, I would say that Windows 10 and Windows Server 2016 are more alike than the Windows 7/Server 2008 combination or the Windows 8/Server 2012 combination.
If you have been using Windows 10, you already have a good head start for successfully interfacing with Windows Server 2016. However, if you are still using older equipment and haven't had a chance to really dive into the latest and greatest operating systems, these big changes in the way that we interact with our servers can be a big stumbling block to successfully utilizing the new tools. Many differences exist when comparing Server 2016 to something like Server 2008, and when you are working within three levels of Remote Desktop Protocol (RDP), bouncing from one server to another, all of these little differences are compounded. It suddenly becomes difficult to know which server it is that you are working on or changing. Let's have a show of hands, how many of you have mistakenly rebooted the wrong server? Or even more likely, how many of you have rebooted your own computer while you were trying to reboot a remote server? I know I have! And not just once.
Hope is not lost! I promise you that, once you learn to manage the interface, rather than letting it manage you, some of these changes may start to seem like good ideas. They can increase productivity and the ease of accomplishing tasksâwe just need some pointers on making the best use of the new interface.
The recipes in this chapter are dedicated to doing just that. Let's work together to gain a better understanding of why the interface was built the way it is, and learn to take advantage of these new screens and settings.
I just couldn't resist starting with this one. Yes, this seems trivial. Silly even. However, the number of times that I have watched a simple server restart consume more mouse clicks than creating a domain controller has convinced me that this needed to be in the book. Perhaps the shutdown and restart options were hidden away purposefully because once your system is up and running, there is not often a need to accomplish either of these tasks. When first configuring the box, though, it is very common to have to reboot a couple of time or to shut down a machine to move it to another location. Let's face it, it doesn't seem to matter how many years computers have been around, many times the magical reboot is still the fixâall answer to most problems, even if we have no idea why.
To go through this recipe, you will need a Windows Server 2016 system online. There are no other prerequisites.
Let's take a look at three different ways to shut down or restart your system. The first is going to be the most commonly employed. The second is still being used by quite a few folks who had to work hard at getting this strange location in their heads during the Windows 8 rollout, and they have continued to use it from that point forward. The third is less commonly known but is by far my favorite when tasked with restarting a remote server.
The first option, thankfully, is in a location that actually makes sense. I say thankfully because when Server 2012 was released, this option didn't exist, and finding the restart function was much more difficult. Just like we have always been able to do prior to the Windows 8 rollout, we can simply click on the Start button and see right there near the bottom that we have Power control options available to us:
Now, when you click on
Shut down or
Restart, you are asked to supply a reason why you are restarting. Common sense tells us that if you are manually clicking on the
Restart button, there is a pretty good chance you are actually intending to restart the server, right? A planned occurrence? But what is the default option that presents itself?
Other (Unplanned). Alas, this silly default option is certainly going to cause us log files full of unplanned restarts, even though all of those restarts were actually planned. Because let's be realânobody takes the time to change that drop-down menu before they click
The second method to accomplish shutting down or restarting is by right-clicking on the Start button. We will discuss this little menu that is presented when right-clicking on Start in our next recipe, but for the sake of a quick shut down or restart, you can simply right-click on the Start button, and then choose
Shut down or sign out:
Each of the previous two examples runs the risk of rebooting the wrong system. Depending on how many layers of remote connections, such as RDP, you are using, it is fairly easy to reboot your own computer or the wrong server instead of the server you intended to reboot, because it is fairly easy to click on the Start button of a different system than the one you intended in the first place. The most definitive, and dare I say the most fun way of restarting your server is to utilize a Command Prompt. Doing this gives you the opportunity to double check that you are manipulating the correct machine. Open up a Command Prompt and run a quick hostname check to make sure you are restarting the one you really intend to. Then utilize the
shutdown command to take care of the rest. This process can be especially helpful when logged into remote servers using RDP. Use the following commands to perform the explained operations:
hostname shutdown /r /t 0
If you were to simply type
shutdown, the server would shut itself down in 60 seconds. Using
/r indicates a restart rather than a shutdown, and
/t 0 is a timing flag that indicates the number of seconds the server should wait before restarting. Specifying slash zero here tells it to wait for zero seconds before initiating the restart:
Shutting down or restarting a server doesn't require a lot of explanation, but I hope that this small recipe gets some thought going about creative ways to do regular tasks. As you will see throughout this book, you can accomplish anything in Windows Server 2016 through the use of commands or scripts. You could easily turn the
shutdown command, the last example that we tested in this recipe, into a batch file, and place it on the Desktop of each of your servers as a quick double-click option for accomplishing this task.
However, I work with RDP windows inside RDP windows very often. When you're bouncing around between a dozen servers that all have the same background image, I have decided that the only sure-fire way to make sure you are restarting the correct device is to do a quick
hostname check before you initiate the restart. If you are interested in discovering all of the available flags that are available to use with the
shutdown command, make sure to type in
shutdown /? some time to take a look at all of the available options.
Earlier versions of Windows Server placed all of the Administrative Tools in a self-named folder right inside the Start menu. This was always a quick and easy place to visit in order to see all of the Administrative Tools installed on a particular server. This location for the tools disappeared as of Server 2012, because of the infamous Start Screen. I am glad to say that a more traditional-looking Start menu has returned in Windows Server 2016, and inside it once again is a link to the
Windows Administrative Tools. However, as you also know there is this thing called
Server Manager that loves to present itself every time that you log in to a server. Since
Server Manager is already on your screen most of the time anyway, it is actually the fastest way to launch these Administrative Tools that you need to utilize so often. Let's take a look at launching your commonly used infrastructure tools right from inside the
Server Manager interface.
All you really need is a Windows Server 2016 machine online. The more roles and services that you have running on it, the more options that you will see on your screen as we navigate these menus.
To launch Administrative Tools from your Desktop, perform the following steps:
- Open up
Server Manager. In fact, if you just logged into the server, it's probably already open for you.
- Click on
Toolsin the upper-right corner.
There you go. A full list of all the Administrative Tools installed onto that server. Heading into this list is also a quick way of taking a look at what a particular server is doing, which you can take an educated guess at based on what roles and services are installed. By looking at the following screenshot, we can see that this server appears to be a domain controller that is also running DNS and DHCP, because all of the related tools are available to choose in this list. That is accurate, as this is my DC1 domain controller server. It is important to note that your server may be running components that do not show up in this list. For example, if you install a role via PowerShell and do not enter the parameter to also install the management tools for that role, it is possible that you could have a server where the role is up and running, but the management tools simply have not been installed. In that case, those tools would not show up in this list:
Since Server Manager likes to open automatically when logging in, let's make quick use of it to open the tools that we need to do our jobs. Another way to have easy access to your tools from the desktop is to create shortcuts or to pin each of them to your taskbar. Sometimes this isn't as easy as it sounds. In the past, these tools were all grouped together in the
Administrative Tools folder, so you didn't have any reason to memorize the exact names of the tools. While you can access them that way again in Server 2016, that folder may or may not appear inside the Start menu depending on how the server is configured, because it appears as one of the live tiles. If you click on the Start button, you could try using the search function to find the tool you are looking for, but its name may not immediately come to you. If you're a consultant working on someone else's server, you may not want to pin anything to their desktop anyway, and you certainly don't want to resort to using Bing in front of them to look up the name of the tool. So I like to stick with launching Administrative Tools from
Server Manager since it always exists, and the tools will always be available inside that menu.
There are some functions in Windows that a server administrator needs to use all the time. Instead of making shortcuts or pinning them all to the taskbar, let's get to know this hidden menu, which is extremely useful for launching these commonly used admin tools.
A running Windows Server 2016 machine is all we need to highlight this one. In fact, this menu also exists on any Windows 10 computer, so make use of it often!
There are two ways to open this little menu. While you are on the Server 2016 desktop, you can perform either of these steps:
- Hold down your Windows key (WinKey) on the keyboard and press X
This little quick-tasks admin menu is very easy to open and is very convenient for launching programs and settings that are accessed often. I won't talk too much about what particulars are in the menu as it's pretty self-explanatory, but I use this menu multiple times per day to open up the
System properties and the
Command Prompt, as it has an option to open an administrative Command Prompt right from the menu.
The Start screen in Windows Server 2012 was not the greatest idea to come out of Microsoft, and unfortunately what it did was train people to no longer click on the Start button, so that we didn't have to deal with the Start screen. Windows 10, and therefore Windows Server 2016, have moved back to a more traditional Start menu, but it is going to take a little bit of time to retrain ourselves to make use of it on a daily basis. I know it will for myself, anyway. Ever since Windows 7 was released, I have been using the Start menu for one critical function in my daily workflow: searching. Let's explore the search capabilities of Server 2016, which can be accessed with a single press of a button.
There are two quick ways that you can search inside Server 2016, and they are right next to each other. If you take a look in the lower-left corner of your screen inside the taskbar, you will see a little magnifying glass next to the Start button. Looks like a search function to me. Click on that button, and you can start typing the name of whatever you would like to search for. In the following screenshot, you can see that I have clicked on my magnifying glass and typed
cmd in order to find the
Command Prompt application:
Search results are presented at the top of that screen, and you can choose what you are looking for accordingly. This is a quick, easy searchâbut I'm not a fan of it because I don't like using my mouse unless I have to. Grabbing my mouse in order to click on the magnifying glass slows down what I'm trying to do while my hands are on the keyboard, so let's take a look at a faster way to search. No matter where you are in Windows Server 2016, no matter what applications you have open, you can always press the WinKey on your keyboard to open up the Start menu, right? What you may not know is that as soon as your Start menu is open, you can immediately start typing anything in order to search for it. If you need to open
Command Prompt, press WinKey and type
cmd. If you need to search for a document called
Text1, press WinKey and type
Text1. I employ this method of opening applications all day every day. This way I don't have to pin anything, I don't have to create any shortcuts, and most importantly, I don't have to use my mouse in order to launch applications:
From the Start menu, we can search for anything on the server. This gives us the ability to quickly find and launch any program or application that we have installed. This includes
Administrative Tools. Rather than moving into
Server Manager in order to launch your administrative consoles from the
Tools menu, you can also search for them on the
Search menu, and launch from there. It also gives us the ability to find files or documents by name. Another powerful way to use the search function in Windows Server 2016 is to open any kind of setting that you might want to change. In previous versions of Windows, you had to either memorize the way to get into the settings that you wanted to change or you had to open up
Control Panel, where you had to poke and prod your way around until you stumbled upon the one that you were looking for. Now it is a very simple matter of pressing the Windows key, typing the first few characters of the setting or program you want to launch, and pressing Enter.
Another common task to perform from the
Search screen is to right-click on the application that you are trying to launch and pin it somewhere. When you right-click on a program from the
Search screen, you see options to pin the program to either your
Start menu or to the taskbar. This will create a quick-launch shortcut on either the main
Start menu or on the taskbar of the desktop mode, giving you easier and faster access to launch those applications in the future.
As you have already noticed,
Server Manager has changed significantly over the past couple of versions of Windows Server. Part of these changes is a shift in mindset where the emphasis is now placed on remote management of servers.
Server Manager in Windows Server 2016 can be used to manage and administer multiple systems at the same time, all from your single pane of glass, the monitor where you are sitting. In this recipe, you are going to learn how to manage both the local server we are logged into, as well as a remote server, from the same Server Manager window.
For this recipe, we need two servers. One is the machine we are physically logged into. Another is a server on the same network that we can contact from our primary server so that we can manage it from our local
- Log in to your primary server and launch
Server Manager. You will see in the upper-left corner that the only server you have listed is the
Local Serverthat we are logged into:
- Now head over toward the top-right of
Server Managerand click on the
Managebutton. In this menu, click on
Add Serversas shown in the following screenshot:
- If your servers are part of a domain, finding remote machines to manage is very easy by simply selecting them from the default
Active Directorytab. If they are not yet joined to your domain, you simply click over to the tab labeled
DNSand search for them from that screen:
- After adding the servers that you want to manage, if you go ahead and click on
All Serversin the left window pane, you will see the additional servers listed that you have selected. If you double-click or right-click on those remote server names, you have many options available to you to remotely manage those machines without having to log into them:
Note that certain servers could resist being manipulated in this way. It is possible to restrict remote management on servers through Group Policy. If that has been done in your environment, you may find that remotely administering them from a centralized console is not possible, and you would have to lift those restrictions on your servers.
Server Manager makes use of the Windows Remote Management (WinRM) tools to remotely manipulate servers. Historically, most of us who administer Windows Servers make extensive use of RDP, often having many windows and connections open simultaneously. This can cause confusion and can lead to tasks being accomplished on servers for which they are not intended. By using
Server Manager from a single machine to manage multiple servers in your network, you will increase your administrative efficiency as well as minimize human error by having all management happen from a single pane of glass.
This recipe is written with the most common network scenario in mind, which is a domain environment where both servers have been joined to the domain. If you are working with standalone servers that are part of a workgroup, rather than being joined to a domain, you will have some additional considerations. In the workgroup scenario, WinRM will need to be enabled specifically, and the Windows Firewall will have to be adjusted in order to allow the right ports and protocols for that WinRM traffic flow to happen successfully. In general, though, most of you will be working within a Microsoft domain network, in which case these items are not necessary.
An incredibly powerful tool in Windows Server 2016 is PowerShell. Think of PowerShell like a Command Prompt on steroids. It is a command-line interface from which you can manipulate almost anything inside Windows that you may care to. Better yet, any task that you may wish to accomplish can be scripted out in PowerShell and saved off as a
.ps1 script file, so that you can automate large tasks and schedule them for later, or at regular intervals. In this recipe, let's open up PowerShell and run some sample commands and tasks just to get a quick feel for the interface. In a later chapter of the book, we will do some more specific tasks with PowerShell to go even deeper into the technology.
To start using PowerShell, all you need is a server with Windows Server 2016 installed. PowerShell is installed and enabled by default.
- PowerShell used to exist in the taskbar by default, which was smart because we really should be pushing people to use it rather than
Command Prompt, right? Unfortunately, PowerShell is not in the taskbar by default in Server 2016, but the Windows Store is...? Explain that one to me some day. So our first step to working in PowerShell is finding it. Thankfully, we know how to search for applications now, so I'll just press my WinKey and type
PowerShell. Once my search result is displayed, I am going to right-click on
Windows PowerShelland choose to
Run as administrator:
- Test out some commands that you are familiar with from using the Command Prompt, such as
cls. Since you are able to make use of these familiar commands, PowerShell can really be your one and only command-line interface if you choose.
- Now let's try some of the PowerShell secret sauce, one of its cmdlets. These are special commands that are built into Windows and allow us to do all kinds of information gathering, as well as manipulation of server components. Let's start by pulling some data. Maybe take a look at what IP addresses are on the system with
- The previous command probably gave you a lot more information than you needed, since most companies don't make use of
IPv6inside their network yet. Let's whittle this information down to the
IPv4-specific info that you are most likely interested in. Enter
Get-NetIPAddress -AddressFamily IPv4to attain it:
PowerShell has so many commands and cmdlets, we just wanted to get a feel for launching the program and pulling some data with this particular recipe. There are countless
Get commands to query information from the server, and as you have seen those cmdlets have various parameters that can be appended to the cmdlets to pull more specific data to meet your needs. To make things even better, there are not only
Get cmdlets, but also
Set cmdlets, which will allow us to make use of the PowerShell prompt to configure many aspects of the configuration on our server, as well as remote servers. We will dive further into PowerShell in a later chapter.
You've installed the Windows Server 2016 operating system onto a piece of hardware. Great! Now what? Without adding roles and features to your server, it makes a great paperweight. We're going to take the next steps here together. Let's install a role and a feature into Windows so that we can start making this server work for us.
As long as you have a Windows Server 2016 installed and running, you are ready to install roles and features onto that machine.
To install a role and a feature into Windows, perform the following steps:
Server Manager. In the middle of the screen, you'll see a link that says
Add roles and features. Click on that link.
Nexton the first summary screen and you will come to a choice on the second page. For most roles and features, we want to leave it set at the top bullet, which is
Role-based or feature-based installation. If we were Configuring Remote Desktop Services, which we will discuss in another chapter, then we would choose the second option.
- Now we choose where we want to install a new role or feature. This is a neat page, as we can choose from any server that we have added into our
Server Manager, or we can even choose to install a role or feature into a virtual hard disk. I am running the
Add Roles Wizardfrom DC1, but I want to install the IIS role onto WEB1. Rather than having to log into WEB1 to accomplish this task, I will do it right from here. In the following screenshot, you can see WEB1 listed as a server that I can install a role onto, even though I am opening this console on the DC1 server:
- Scroll down and choose the role that you want to install. For WEB1, I am choosing the
Web Server (IIS)role. Then click
You can install more than one role or feature at a time. Some roles require additional components to be installed for them to work properly. For example, when I chose to install the IIS role and clicked
Next, I was prompted about needing to install some management tools. Simply click on the
Add Features button to automatically add the items that it needs to perform correctly.
- Now choose any features that you would like to install. For example, in order to do some network connectivity testing later, go ahead and select
Telnet Clientfrom the list.
- Read and click
Nextthrough the informational messages that are displayed. These messages will vary depending on which roles and features you have installed.
- The final screen is your installation summary. If everything looks correct, go ahead and click on
After your roles and features have finished installing, the server may or may not have to reboot. This depends on whether or not the role installation requires it. Following installation, or following the reboot, if your new role needs any additional configuration or setting up to be completed, you will be notified at the top of the Server Manager screen.
Adding roles and features to a Windows Server is something that every administrator will have to do sooner or later. These items are necessary to turn on the functions in the server that will perform tasks that need to be performed in your environment. Adding roles is quite straightforward. However, it is interesting to see the options that are available to add more than one role or feature at a time. Moreover, the ability to remotely install these items for servers in your network that you are not logged into is intriguing.
In the Managing remote servers from a single pane with Server Manager recipe, we discussed remotely administering another server by using Server Manager. Did you know we can accomplish the same remote management by using our day-to-day Windows 10 computer? We will install and use the Remote Server Administration Tools (RSAT) to take even more advantage of Server 2016's remote management ideology.
To test out the RSAT tools, we will need a Windows 10 client machine. We will then also need a Windows Server 2016 system online, and on the same network, which we can remotely control and manage.
- First, we need to download the RSAT tools. You can use Bing to search for Remote Server Administration Tools for Windows 10, or use this link to download RSAT for Windows 10: https://www.microsoft.com/en-us/download/details.aspx?id=45520. Here is also the link for the same RSAT tools in the Windows 8.1 flavor: http://www.microsoft.com/en-us/download/details.aspx?id=39296. After you install these tools onto your Windows 10 or 8.1 computer, you should now have a copy of Server Manager installed onto your computer. Go ahead and launch that from the Start menu. You can pin it to your Taskbar for quicker launching in the future, of course. In the same fashion, as with Server 2016, you can use the
Managemenu to add servers to Server Manager:
- For this recipe, I do have the machines we are working with joined to a domain, so we will take a look at adding servers that are part of the domain.
- Click on the
Find Nowbutton and you will see a list of server names that are remotely manageable:
- Click on the server names that you want to administer and click on the arrow to move them over to the right side of the screen. Upon clicking on
OK, you will see these new servers listed and ready for management inside your
Server Manager in Windows Server 2016 is a powerful tool that can be used for the management of not only the local server but also remote servers that you want to manage. If we take this even a step further and install the RSAT tools on a Windows 10 computer, this gives us the ability to launch and use
Server Manager from our everyday Windows 10 computer. In doing so, we enable ourselves to add roles, view events, and restart servers, all from our own desk. Managing servers using these tools will increase productivity and decrease errors because your entire infrastructure of servers can be available within a single window. This is much more efficient than using the RDP client to connect to many different servers, all in different windows. If you've never tried using RSAT to manage servers, give it a try!
I prefer using a keyboard over a mouse any day, for almost any task. There are numerous keyboard shortcuts and tips and tricks that I employ on a daily basis and I want to test them out with you in this recipe. Some of these shortcuts have been around for years and will work with multiple versions of Windows Server; some are new in the Server 2016 operating system. They will all be useful to you as you start working with servers in your network.
We are going to run these commands and keyboard shortcuts while logged into a Windows Server 2016 machine.
- Windows key: Opens the
Startmenu, where you can immediately start typing to search for programs.
- Windows key + X: Opens the Quick Links menu, which we discussed in an earlier recipe.
- Windows key + I: Opens Windows
- Windows key + D: Minimizes all open windows and brings you back to the Desktop.
- Windows key + R: Opens the
Runbox. Launching applications this way is often faster than using the Start menu, if you know the executable name of the application you are trying to launch.
- Windows key + M: Minimizes all windows.
- Windows key + E: Opens File Explorer.
- Windows key + L: Locks the computer.
- Windows key + Tab: Takes you into the new Task View options.
- Window key + Ctrl + D: Creates a new virtual Desktop from Task View.
- Windows key + Ctrl + F4: Closes the current virtual Desktop.
- Windows key + Ctrl + Left or Right Arrow: Move between different virtual Desktops.
- Windows key + 1 or 2 or 3 or...: Launches applications that are pinned to your taskbar, in order. So the first application pinned to the taskbar would open with WinKey + 1, for example.
- Alt + F4: Exits the program you are currently working in. This is especially helpful in full-screen appsâlike those from the Windows Store - where it is not always obvious how to exit the program with your mouse.
- Alt + Tab: Displays a list of open programs so you can hop between them.
- Shift + Delete: Holding down Shift while pressing Delete deletes files without placing them into the Recycle Bin.
- Using Tab inside Command Prompt or PowerShell: I cannot believe that I went years without knowing about this one. When you are working inside Command Prompt, if you type the first letter of a file or folder that exists in the directory where you are working and then press the Tab key, it will auto-populate the rest of the filename. For example, you may be trying to launch a Microsoft update file with a filename that is 15 characters and comprises a mix of numbers and letters. No need to type out that filename! Let's say the file starts with
KB. Simply navigate to the folder where your installer exists, type
KB, and press Tab. The full filename is populated inside Command Prompt and you can press the Enter key to launch it.
Keyboard shortcuts can greatly increase productivity once you are fluent with them. This is not an extensive list by any means, there are many more key combinations that you can use to launch apps, minimize and maximize windows, and do all sorts of other functions. This is a list to get you started with the most common ones that I employ often. Start using these with your daily tasks and I bet your mouse will start to feel lonely.
If you are interested in exploring more of the Windows Server 2016 key combinations available, this website is a great place to start: http://technet.microsoft.com/en-us/library/hh831491.aspx.
To say that the Windows operating system can be manipulated by PowerShell is a gross understatement. They are fully intertwined, and PowerShell can be useful for so many tasks on your servers. However, the ability to run PowerShell scripts is disabled by default on many machines. The first stumbling block that many new PowerShell administrators bump into is the Execution Policy. It's quite simple: in order to allow PowerShell scripts to run on your server, the Execution Policy must be adjusted to allow that to happen. Let's introduce our first task in PowerShell by using some commands in this recipe that will set this policy for us.
This is also a good introduction to the idea of the verb-noun syntax that PowerShell utilizes. For example, we are going to make use of cmdlets called
parameter name) and
parameter name) cmdlets are very common across all facets of cmdlets available in PowerShell. Wrap your mind around this verb-noun syntax and you will be well on your way to figuring out PowerShell on your machines.
- Right-click on the PowerShell icon and choose
Run as administrator:
Get-ExecutionPolicyand press Enter in order to see the current setting of the PowerShell Execution Policy:
- You can see that the current Execution Policy is set to
RemoteSigned. Here is a short description of the different options for the policy:
- Remote Signed: This is the default setting in Server 2016, which allows PowerShell scripts that are locally created to run. If you try running remote scripts, they must be signed by a trusted publisher in order to execute successfully.
- All Signed: With this setting, all scripts will only be allowed to run if they are signed by a trusted publisher.
- Restricted: With this setting, PowerShell is locked down so that scripts will not run.
- Unrestricted: This setting will allow PowerShell to run scripts, with or without signing.
- For the purposes of our recipe and to make sure scripts will run for us as we progress through these recipes, let's set our Execution Policy to unrestricted. Go ahead and use this command:
The PowerShell Execution Policy is a simple setting and easy to change, but can make a world of difference when it comes to running your first scripts. If configured to be more restrictive than you intend, you will have trouble getting your scripts to run and may think that you have mistyped something, when in fact the issue is only the policy. On the other hand, in an effort to make your servers as secure as possible, on machines where you don't need to execute PowerShell scripts, it makes sense to restrict this access. You may also want to read some additional information on the signing of scripts to see whether creating and executing signed scripts would make more sense in your own environment. There are some in-built server functions that rely on a certain level of security with your Execution Policy. Setting your policy to unrestricted on all of your servers could result in some functions not working properly, and you may have to increase that level of security back to remote signed.
Command Prompt and PowerShell are both great command-line interfaces that can acquire and configure information about our servers. Most of us are familiar with creating some simple batch files that are driven by
Command Prompt, essentially programming out small tasks within these batch files to automate a series of commands. This saves time later as we do not have to type out the commands line by line, especially for common tasks or for items that we need to run during login.
PowerShell has similar functionality, the ability to write out multiple lines of PowerShell cmdlets inside a script file. We can then launch this script file as we would a batch file, automating tasks while taking advantage of the additional features that PowerShell brings to the table over
Command Prompt. These PowerShell scripts are put together inside
.ps1 files; let's build a simple one together to get a feel for running these scripts.
Our work with PowerShell today will be accomplished from a Windows Server 2016 machine. PowerShell is installed by default with Windows, and there is nothing further that we need to install.
Follow these steps to build and execute our first PowerShell script:
- Open the Start menu and type
Windows PowerShell ISE. Right-click to launch this tool as an administrator.
Windows PowerShell ISEis an editor for PowerShell scripts that is much more useful than opening a simple text editor such as Notepad in order to build our script:
- Navigate to
Newfrom the menus in order to open a blank
- In your first line, type the following:
Write-Host "Hello! Here is the current date and time:".
- From the toolbar menu, click the green arrow that says
Run Script. Alternatively, you can simply press the F5 button. When you run the script, the command and output are displayed in the lower portion of the ISE window:
Cool! Okay, so far it's actually pretty lame. It's just reflecting the text that we told it to echo, but it worked. That is the nice thing about using the ISE editing tool rather than a generic text editor, you have the ability to quickly test run scripts as you make modifications.
- Now let's add some additional lines into our script to give us the information we are looking for. You can see a list of available commands on the right side of the screen if you would like to browse through what is available, but for our example simply change your script to include the following:
Write-Host "Hello! Here is the current date and time:" Get-Date Write-Host "The name of your computer is:" hostname
- Press the
Run Scriptbutton again to see the new output:
- Now navigate to
Saveand save your new
.ps1PowerShell script out to the Desktop.
- Let's test this script by launching it from within a real PowerShell command window. Right-click on your PowerShell icon in the Taskbar and choose
Run as administrator.
- Browse to the location of the script file, I placed mine on the Desktop. Then launch the script by inputting
.filename. In my case, it looks like this:
Remember that the Tab key can be our friend in this. When browsing to your Desktop, all you need to do is input the first letter of your script filename, and then press Tab. Since I named my script
Time.ps1, all I had to do was press the T and then press Tab, then Enter.
In this recipe, we created a very simple PowerShell script and saved it on our server for execution. While in practice getting time and date information from your server may come faster by using the standalone
Get-Date cmdlet, we use this recipe to give a small taste of the ISE and to get your scripting juices flowing. Expanding upon the ideas presented here will start to save you valuable time and keystrokes as you identify more and more ways to automate the tasks and information gathering that are part of your daily routines. The possibilities of PowerShell are practically limitless, so make sure that you open it up and start becoming familiar with the interfaces and tools associated with it right away!
With this recipe, let's take a minute to use
Get-Help inside PowerShell in order to, well, get some help! I see both new and experienced PowerShell administrators going to the Web a lot in order to find commands and the parameters of those commands. The Internet is great, and there is a ton of data out there about how to use PowerShell, but in many cases the information that you are looking for resides right inside PowerShell itself. By using the
Get-Help cmdlet combined with the functions you are running or searching for, you might not have to open that web browser after all.
To use the
Get-Help function inside PowerShell, run the following steps:
- Launch a PowerShell prompt.
- You're finished! No, I'm just kidding. Using
Get-Helpby itself will present you with some helpful data about the
Get-Helpcommand, but that's not really what we are looking for, is it? How about using
Get-Helpwith a search parameter, like this:
Cool! That searched the available cmdlets and presented us with a list of the ones that contain the word
- Now, what if we wanted to find out some more particular information about one of these cmdlets? Maybe about
Restart-Computer; that sounds like something we might use often. Use the following command:
Now we're really cooking! This is wonderful information. Basically, this is exactly what you would find if you were looking for information about the
Restart-Computer cmdlet and went searching on TechNet for it.
Get-Help cmdlet in PowerShell can be used with virtually any command in order to find out more information about that particular function. I often use it when the specific name of a cmdlet that I want to use escapes my memory. By using
Get-Help as a search function, it will present a list of available cmdlets that include the keyword you specified. This is a brilliant addition to PowerShell, and makes it so much more powerful than Command Prompt.
Also included with the
Get-Help files are all of the special syntax and parameter options for each cmdlet that you might be working with. This saves you having to go to the web in order to search for these functions, and it is just way more fun doing it at the command line than in a web browser.