0% found this document useful (0 votes)
6 views5 pages

Classwork 7

The document provides a tutorial on using PowerShell, focusing on creating and managing profile scripts, variable syntax, and best practices for string manipulation. It includes specific commands for creating variables, displaying processes, and modifying strings using the -replace operator. Exercises at the end reinforce the concepts covered in the tutorial.

Uploaded by

Kevin Hoag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Classwork 7

The document provides a tutorial on using PowerShell, focusing on creating and managing profile scripts, variable syntax, and best practices for string manipulation. It includes specific commands for creating variables, displaying processes, and modifying strings using the -replace operator. Exercises at the end reinforce the concepts covered in the tutorial.

Uploaded by

Kevin Hoag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Kevin Hoag

Classwork 7

Background and Syntax Review


Part One
Let us use the New-Item cmdlet along with the $profile automatic variable to create our
profile script:
New-Item –Type File –Path $PROFILE –Force
Now that the $profile variable actually maps to an empty file, we can run the following
command to pop open the script file in good old Windows Notepad:
Notepad $profile
Now type these commands and save the file:

Set-Location C:\
Clear-Host
New-Alias –Name np –Value “C:\Windows\Notepad.exe”

Part Two

Remember that variables’ names do not include a dollar sign:

Var refers to the variable itself, meaning the container in which you can put things.

$Var refers to whatever is inside the container. For example:

PS C:\> $Var = "x" PS C:\> New-Variable –Name $Var –Value 7

That creates a new variable, $x, containing the value 7. Keep looking atthe
example until you’re sure you understand why.

In double quotation marks, PowerShell replaces variables with their contents:

PS C:\> $msg = "Student"

PS C:\> $text = "Hello, $msg"

PS C:\> $text Hello, Student

However, this only works with variables – you cannot use this same technique to
access properties, array indices, etc. To do those, you must use a
Kevin Hoag
Classwork 7
subexpression:

PS C:\> $services = Get-Service

PS C:\> $message = "The name of the first service is $($services[0].name)"

PS C:\> $message

The name of the first service is AeLookupSvc

Using these techniques is a better practice than concatenating strings and


variables.

Remember that you can declare a type for your variables. Common types include
[string], [int], [bool], [xml], [datetime], and others.

PS C:\> [int]$x = 5

PS C:\> $x = "Hello"

Cannot convert value "Hello" to type "System.Int32".

Error: "Input string was not in a correct format." At line:1 char:1 + $x = "Hello" +
~~~~~~~~~~~~ + CategoryInfo : MetadataError: (:) [],
ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException

PS C:\> [string]$x = "Hello" PS C:\> $x Hello


Kevin Hoag
Classwork 7

Exercises
Write down your answers for the following tasks:

1. Display a list of all currently defined variables.

Get-Variable

2. Create a variable named x and populate it with the number 100.

$x = 100

3. Completely remove the variable named x.

Remove-Variable x
Kevin Hoag
Classwork 7

4. Place all running processes into a new variable named procs.

$procs = Get-Process

5. Display the first object in $procs.

$procs[0]

6. Display the name of the first object in $procs.

$procs[0].Name

7. Create a message, “The first proc name is ___”. Store the


message in $message, and insert the name of the first object in
$procs in place of the ___.

$message = "The first proc name is $($procs[0].Name)"


Kevin Hoag
Classwork 7

8. Use the –replace operator to replace “proc” with “process” in


$message. Read about_comparison_operators for more
information on the –replace operator.

$message = $message -replace 'proc', 'process'

You might also like