Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Server 2012 R2 Administrator Cookbook

You're reading from   Windows Server 2012 R2 Administrator Cookbook Over 80 hands-on recipes to effectively administer and manage your Windows Server 2012 R2 infrastructure in enterprise environments

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781784393076
Length 310 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Jordan Krause Jordan Krause
Author Profile Icon Jordan Krause
Jordan Krause
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Learning the Interface FREE CHAPTER 2. Core Infrastructure Tasks 3. Security and Networking 4. Working with Certificates 5. Internet Information Services 6. Remote Access 7. Remote Desktop Services 8. Monitoring and Backup 9. Group Policy 10. File Services and Data Control Index

Building and executing your first PowerShell script

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.

Getting ready

Our work with PowerShell today will be accomplished from a Windows Server 2012 R2 machine. PowerShell is installed by default with Windows, and there is nothing further that we need to install.

How to do it...

Follow these steps to build and execute our first PowerShell script:

  1. Open the Start screen and type Windows PowerShell ISE. Right-click to launch this tool as an administrator. 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.
    How to do it...
  2. Choose File | New from the menus in order to open a blank .ps1 script file.
  3. In your first line, type the following: Write-Host "Hello! Here is the current date and time:".
  4. 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.

    How to do it...

    Cool! Okay, so far it's actually pretty lame. 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.

  5. 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
    
  6. Press the Run Script button again to see the new output.

    How to do it...
  7. Now use File | Save and save your new .ps1 PowerShell script out to the Desktop.
  8. 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.
  9. 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: .\time.ps1.
    How to do it...

How it works...

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 stand-alone 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 behind 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!

You have been reading a chapter from
Windows Server 2012 R2 Administrator Cookbook
Published in: Jan 2015
Publisher:
ISBN-13: 9781784393076
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