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:
Windows PowerShell ISE
. Right-click to launch this tool as an administrator. Windows PowerShell ISE is 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.
.ps1
script file.Write-Host "Hello! Here is the current date and time:"
.
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.
Write-Host "Hello! Here is the current date and time:"
Get-Date
Write-Host "The name of your computer is:"
hostname
.ps1
PowerShell script out to the Desktop..filename
. In my case, it looks like this: . ime.ps1
.
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!
18.119.160.42