Movatterモバイル変換


[0]ホーム

URL:


Packt
Search iconClose icon
Search icon CANCEL
Subscription
0
Cart icon
Your Cart(0 item)
Close icon
You have no products in your basket yet
Save more on your purchases!discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Profile icon
Account
Close icon

Change country

Modal Close icon
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timerSALE ENDS IN
0Days
:
00Hours
:
00Minutes
:
00Seconds
Home> Cloud & Networking> Shell Scripting> Mastering Windows PowerShell Scripting
Mastering Windows PowerShell Scripting
Mastering Windows PowerShell Scripting

Mastering Windows PowerShell Scripting: Master the art of automating and managing your Windows environment using PowerShell

Arrow left icon
Profile Icon Brenton J.W. Blawat
Arrow right icon
₹3276.99
Full star iconFull star iconFull star iconFull star iconHalf star icon4.4(16 Ratings)
eBookApr 2015282 pages1st Edition
eBook
₹3276.99
Paperback
₹4096.99
Hardcover
₹4096.99
Subscription
Free Trial
Renews at ₹800p/m
eBook
₹3276.99
Paperback
₹4096.99
Hardcover
₹4096.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
OR

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Table of content iconView table of contentsPreview book icon Preview Book

Mastering Windows PowerShell Scripting

Chapter 2. Data Parsing and Manipulation

One of the most powerful features of PowerShell is its ability to retrieve and manipulate data. Many a times when you retrieve data from a PowerShell session, the format in which it is available is different from what you would want to display in the PowerShell window or in a log file. For this purpose, PowerShell provides powerful cmdlets and methods to perform data manipulation to best suit your needs as a PowerShell scripter.

While reading this chapter, you'll learn the following concepts:

  • String manipulation
  • Number manipulation and parsing
  • Date/time manipulation
  • Forcing data types
  • PowerShell pipeline

String manipulation

String manipulationis something that you'll need to do in almost every script you create. While some of the string methods will be used more often than others, they all serve different purposes for your script. It is ultimately up to your creativity on how you want data to look when it is displayed on the screen.

To change the text to uppercase, execute the following command:

$a = "Error: This is an example error"$a.toUpper()

The output of this is shown in the following screenshot:

String manipulation

ThetoUpper() method is used to format the text to uppercase. This is helpful in situations where messages need to be emphasized or should stand out. The result of this command will change the case.

To change the string to lowercase, execute the following command:

$string = "The MAC Address is "$mac = "00:A0:AA:BB:CC:DD"$message = $string + $mac.toLower()$message

The output of this is shown in the following screenshot:

String manipulation

The inverse oftoUpper() is the use...

Number manipulation and parsing

PowerShell is a powerfulmathematics calculator. In fact, PowerShell has an entire Windows class dedicated to mathematics calculations that can be called by using the[System.Math] .NET class. When you are working with the[System.Math] classes, it is common to callstatic fields within a class. Static fields are static properties, methods, and objects that can be called to display data or do actions. To call a static field, you call the[Math](shortened version of[System.Math]) class, followed by two colons:: and the static field name.

To use the math operation to calculate pi, execute the following command:

[math]::pi

The output of this is shown in the following screenshot:

Number manipulation and parsing

This simple command will providePI if you ever need it for a calculation by using thepimethod of the math class. The result of this command returns3.14159265358979.

To use the math operation to calculate Euler's number, execute the following command:

[math]::e

The output of this...

Date and time manipulation

When you arescripting, there are times where you may need to get the date andtime of a system. PowerShell offers theget-date cmdlet, which provides the date and time in many different formats of your choice.

To obtain the date object, execute the following command:

$time = get-date$time

The output of this is shown in the following screenshot:

Date and time manipulation

The standardget-date cmdlet, without any triggers, will generate the long date and time format. When you store thedate object in a variable, it is important to remember that the data captured from the cmdlet is a snapshot in time. You'll have to call theget-date cmdlet again to get new values for the updated date and time.

The following table displays all of the date time formatting codes:

Format code

Result

Example

MM

Month in numeric format

04

DD

Day in numeric format

15

YYYY

Year in numeric format

2014

HH

Hour in numeric format (24hrs)

14

hh

Hour in numeric format (12hrs)

02

mm

Minutes in numeric...

Forcing data types

While developing scripts, youmay run into instances where you may want to force a specific data type. This is helpful in cases where PowerShell automatically interprets the output from a command incorrectly. You can force data types by the use of brackets specifying a data type and a variable.

To force a string data type, execute the following command:

[string]$myString = "Forcing a String Container"$myString

The output of this is shown in the following screenshot:

Forcing data types

The preceding command forces the string data type to the$myString variable. The result is that the$myString variable will always remain a string. It is important to know that if the object or item that you are trying to force to a data type doesn't have a direct conversion to that data type, it will throw an error or exception. This would be the case if you try to insert a string into an integer data type.

To force a string data type and generate a data exception, execute the following command...

Piping variables

The concept of piping isn't anything new to the scripting world. Piping, by definition, is directing the output of an object to another object. When you use piping in PowerShell, you are taking the output of one command and sending the data for use with another section of code. The manipulation can be either to a more legible format, or can be byselecting a specific object and digging deeper into those attributes. A pipe is designated by the '|' symbol and is used after you enter a command. The construct of a pipe looks like this:command | ResultManipulation | SortingObjects. If you need to access the individual items in the pipeline, you can leverage the pipeline output$_ command. This tells the pipeline to evaluate the results from the pipeline and their attributes.

The pipeline offers a wide variety of uses; you can leverage commands such assort-object to sort by a specific attribute,format-list to format the objects into a list, and even theselect...

String manipulation


String manipulationis something that you'll need to do in almost every script you create. While some of the string methods will be used more often than others, they all serve different purposes for your script. It is ultimately up to your creativity on how you want data to look when it is displayed on the screen.

To change the text to uppercase, execute the following command:

$a = "Error: This is an example error"$a.toUpper()

The output of this is shown in the following screenshot:

ThetoUpper() method is used to format the text to uppercase. This is helpful in situations where messages need to be emphasized or should stand out. The result of this command will change the case.

To change the string to lowercase, execute the following command:

$string = "The MAC Address is "$mac = "00:A0:AA:BB:CC:DD"$message = $string + $mac.toLower()$message

The output of this is shown in the following screenshot:

The inverse oftoUpper() is the use oftoLower(). This command will convert...

Left arrow icon

Page1 of 7

Right arrow icon

Description

If you are a system administrator who wants to become an expert in controlling and automating your Windows environment, then this book is for you. Prior knowledge of PowerShell's core elements and applications is required for this book.

What you will learn

  • Utilize variables, hashes, and arrays to store data
  • Parse and manipulate different data types
  • Optimize code through the use of functions, switches, and looping structures
  • Create and implement regular expressions in PowerShell scripts
  • Leverage sessionbased remote management
  • Manage files, folders, and registries through the use of PowerShell
  • Discover the best practices to manage Microsoft systems

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date :Apr 27, 2015
Length:282 pages
Edition :1st
Language :English
ISBN-13 :9781782173564
Vendor :
Microsoft
Languages :

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
OR

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Product Details

Publication date :Apr 27, 2015
Length:282 pages
Edition :1st
Language :English
ISBN-13 :9781782173564
Vendor :
Microsoft
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800billed monthly
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconSimple pricing, no contract
₹4500billed annually
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconChoose a DRM-free eBook or Video every month to keep
Feature tick iconPLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick iconExclusive print discounts
₹5000billed in 18 months
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconChoose a DRM-free eBook or Video every month to keep
Feature tick iconPLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick iconExclusive print discounts

Frequently bought together


Active Directory with PowerShell
Active Directory with PowerShell
Read more
Jan 2015230 pages
Full star icon4 (9)
eBook
eBook
₹2919.99
₹3649.99
Mastering Windows PowerShell Scripting
Mastering Windows PowerShell Scripting
Read more
Apr 2015282 pages
Full star icon4.4 (16)
eBook
eBook
₹3276.99
₹4096.99
₹4096.99
Getting Started with Powershell
Getting Started with Powershell
Read more
Aug 2015180 pages
Full star icon1 (1)
eBook
eBook
₹2621.99
₹3276.99
Stars icon
Total11,023.97
Active Directory with PowerShell
₹3649.99
Mastering Windows PowerShell Scripting
₹4096.99
Getting Started with Powershell
₹3276.99
Total11,023.97Stars icon

Table of Contents

15 Chapters
1. Variables, Arrays, and HashesChevron down iconChevron up icon
1. Variables, Arrays, and Hashes
Variables
Arrays
Hashes
Deciding the best container for your scripts
Summary
2. Data Parsing and ManipulationChevron down iconChevron up icon
2. Data Parsing and Manipulation
String manipulation
Number manipulation and parsing
Date and time manipulation
Forcing data types
Piping variables
Summary
3. Comparison OperatorsChevron down iconChevron up icon
3. Comparison Operators
Comparison operator basics
Equal and not equal comparison
Greater than and less than comparison
Contains, like, and match operators
And / OR comparison operators
Best practices for comparison operators
Summary
4. Functions, Switches, and Loops StructuresChevron down iconChevron up icon
4. Functions, Switches, and Loops Structures
Functions
Looping structures
Switches
Combining the use of functions, switches, and loops
Best practices for functions, switches, and loops
Summary
5. Regular ExpressionsChevron down iconChevron up icon
5. Regular Expressions
Getting started with regular expressions
Regular expression grouping constructs and ranges
Regular expression quantifiers
Regular expression anchors
Regular expressions examples
Summary
6. Error and Exception Handling and Testing CodeChevron down iconChevron up icon
6. Error and Exception Handling and Testing Code
Error and exception handling – parameters
Error and exception handling – Try/Catch
Error and exception handling – legacy exception handling
Methodologies for testing code
Summary
7. Session-based Remote ManagementChevron down iconChevron up icon
7. Session-based Remote Management
Utilizing CIM sessions
Summary
8. Managing Files, Folders, and Registry ItemsChevron down iconChevron up icon
8. Managing Files, Folders, and Registry Items
Registry provider
Creating files, folders, and registry items with PowerShell
Adding named values to registry keys
Verifying files, folders, and registry items
Copying and moving files and folders
Renaming files, folders, registry keys, and named values
Deleting files, folders, registry keys, and named values
Summary
9. File, Folder, and Registry Attributes, ACLs, and PropertiesChevron down iconChevron up icon
9. File, Folder, and Registry Attributes, ACLs, and Properties
Retrieving attributes and properties
Viewing file and folder extended attributes
Setting the mode and extended file and folder attributes
Managing file, folder, and registry permissions
Summary
10. Windows Management InstrumentationChevron down iconChevron up icon
10. Windows Management Instrumentation
WMI structure
Using WMI objects
Searching for WMI classes
Creating, modifying, and removing WMI property instances
Invoking WMI class methods
Summary
11. XML ManipulationChevron down iconChevron up icon
11. XML Manipulation
XML file structure
Summary
12. Managing Microsoft Systems with PowerShellChevron down iconChevron up icon
12. Managing Microsoft Systems with PowerShell
Managing local users and groups
Managing Windows services
Managing Windows processes
Installing Windows features and roles
Summary
13. Automation of the EnvironmentChevron down iconChevron up icon
13. Automation of the Environment
Invoking programs for automation
Using desired state configuration
Detecting and restoring drifting configurations
Summary
14. Script Creation Best Practices and ConclusionChevron down iconChevron up icon
14. Script Creation Best Practices and Conclusion
Best practices for script management
# commenting headers
Best practices for script creation
Best practices for software automation
Summary
IndexChevron down iconChevron up icon
Index

Recommendations for you

Left arrow icon
Solutions Architect's Handbook
Solutions Architect's Handbook
Read more
Mar 2024582 pages
Full star icon4.6 (57)
eBook
eBook
₹3574.99
₹4468.99
Mastering Terraform
Mastering Terraform
Read more
Jul 2024506 pages
Full star icon5 (21)
eBook
eBook
₹2978.99
₹3723.99
The Ultimate Linux Shell Scripting Guide
The Ultimate Linux Shell Scripting Guide
Read more
Oct 2024696 pages
Full star icon4.9 (7)
eBook
eBook
₹2978.99
₹3723.99
Mastering Windows PowerShell Scripting
Mastering Windows PowerShell Scripting
Read more
Feb 2019626 pages
Full star icon5 (26)
eBook
eBook
₹3276.99
₹4096.99
₹3351.99
Kubernetes – An Enterprise Guide
Kubernetes – An Enterprise Guide
Read more
Aug 2024682 pages
Full star icon4.8 (13)
eBook
eBook
₹3276.99
₹4096.99
The Self-Taught Cloud Computing Engineer
The Self-Taught Cloud Computing Engineer
Read more
Sep 2023480 pages
Full star icon5 (180)
eBook
eBook
₹2978.99
₹3723.99
CI/CD Design Patterns
CI/CD Design Patterns
Read more
Dec 2024356 pages
eBook
eBook
₹2382.99
₹2978.99
Platform Engineering for Architects
Platform Engineering for Architects
Read more
Oct 2024374 pages
Full star icon5 (2)
eBook
eBook
₹2978.99
₹3723.99
Microsoft Azure Fundamentals Certification and Beyond
Microsoft Azure Fundamentals Certification and Beyond
Read more
Jan 2024284 pages
Full star icon4.8 (29)
eBook
eBook
₹2680.99
₹3351.99
The Ultimate Docker Container Book
The Ultimate Docker Container Book
Read more
Aug 2023626 pages
Full star icon4 (8)
eBook
eBook
₹2978.99
₹3723.99
Right arrow icon

Customer reviews

Top Reviews
Rating distribution
Full star iconFull star iconFull star iconFull star iconHalf star icon4.4
(16 Ratings)
5 star75%
4 star12.5%
3 star0%
2 star6.3%
1 star6.3%
Filter icon Filter
Top Reviews

Filter reviews by




MeMay 23, 2015
Full star iconFull star iconFull star iconFull star iconFull star icon5
I have numerous books on PowerShell and have been working with PowerShell for only a few months. But this is the first book on PowerShell that I can say truly speaks to me. The light bulb came on as I was reading this book. It is well written and organized in such a manner that I can honestly see myself taking my PowerShell skills to the next level. It is an absolutely great book that I would highly recommend to anyone who also wants to take their PowerShell skills to the next level. Each chapter made me want to continue reading more and had me looking forward to the next chapter. I am also looking forward to introducing this book to all of my co-workers with the hope that they also will build on their knowledge of PowerShell. This book has earned a permanent place on my bookshelf. Thank you for this book…
Amazon Verified reviewAmazon
Brenton BlawatMay 07, 2015
Full star iconFull star iconFull star iconFull star iconFull star icon5
After a years worth of work, I finally have the Mastering Windows PowerShell Scripting book complete. It is absolutely everything you need to know to become a PowerShell Master in your environment. I hope you enjoy the book as much as I enjoyed writing it!
Amazon Verified reviewAmazon
Todd M. WilliamsenJun 15, 2016
Full star iconFull star iconFull star iconFull star iconFull star icon5
I personally have worked with the author at a mutual client. Brenton is extremely willing to help at any costs, and he exhibits the same motive in this book.
Amazon Verified reviewAmazon
Jason PDec 02, 2015
Full star iconFull star iconFull star iconFull star iconFull star icon5
I am starting out learning PowerShell for my job and this book helped me out a ton. Great book, well written, excellent guide.
Amazon Verified reviewAmazon
KenzoSep 02, 2016
Full star iconFull star iconFull star iconFull star iconFull star icon5
great foundational fountain of knowledge for powershell
Amazon Verified reviewAmazon
  • Arrow left icon Previous
  • 1
  • 2
  • 3
  • 4
  • Arrow right icon Next

People who bought this also bought

Left arrow icon
Mastering Ubuntu Server
Mastering Ubuntu Server
Read more
Sep 2022584 pages
Full star icon4.6 (36)
eBook
eBook
₹3276.99
₹4096.99
The Ultimate Docker Container Book
The Ultimate Docker Container Book
Read more
Aug 2023626 pages
Full star icon4 (8)
eBook
eBook
₹2978.99
₹3723.99
Mastering Kubernetes
Mastering Kubernetes
Read more
Jun 2023746 pages
Full star icon4.6 (45)
eBook
eBook
₹3276.99
₹4096.99
Ansible for Real-Life Automation
Ansible for Real-Life Automation
Read more
Sep 2022480 pages
Full star icon3.9 (7)
eBook
eBook
₹2502.99
₹3127.99
AWS for Solutions Architects
AWS for Solutions Architects
Read more
Apr 2023696 pages
Full star icon4.3 (62)
eBook
eBook
₹4766.99
₹5958.99
Right arrow icon

About the author

Profile icon Brenton J.W. Blawat
Brenton J.W. Blawat
Brenton J.W. Blawat is an entrepreneur, strategic technical advisor, author, and enterprise architect, who has a passion for the procurement of technology in profit-based organizations. He is business-centric and technology-minded. Brenton has many years of experience in bridging the gap between technical staff and decision-makers in several organizations. He takes pride in his ability to effectively communicate with a diverse audience and provide strategic direction for large and small organizations alike._x000D_ In 2013, Brenton authored his first book, PowerShell 3.0 WMI Starter, Packt Publishing. In March 2015, he authored his second book Mastering Windows PowerShell Scripting, Packt Publishing._x000D_ Brenton currently works at CDW as an Enterprise Architect in strategic solutions and services. CDW is a leading multibrand technology solutions provider in the fields of business, government, education, and healthcare. A Fortune 500 company, it was founded in 1984 and employs approximately 7,200 coworkers. In 2016, the company generated net sales of more than $13.0 billion._x000D_ His current specialisation sits on top of 15 years experience spread across (predominantly Microsoft) systems, (Juniper and Cisco) networking, and security.
Read more
See other products by Brenton J.W. Blawat
Getfree access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook?Chevron down iconChevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website?Chevron down iconChevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook?Chevron down iconChevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support?Chevron down iconChevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks?Chevron down iconChevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook?Chevron down iconChevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.

Create a Free Account To Continue Reading

Modal Close icon
OR
    First name is required.
    Last name is required.

The Password should contain at least :

  • 8 characters
  • 1 uppercase
  • 1 number
Notify me about special offers, personalized product recommendations, and learning tips By signing up for the free trial you will receive emails related to this service, you can unsubscribe at any time
By clicking ‘Create Account’, you are agreeing to ourPrivacy Policy andTerms & Conditions
Already have an account? SIGN IN

Sign in to activate your 7-day free access

Modal Close icon
OR
By redeeming the free trial you will receive emails related to this service, you can unsubscribe at any time.

[8]ページ先頭

©2009-2025 Movatter.jp