0% found this document useful (0 votes)
23 views7 pages

PowerShell Basics for System Administration

Uploaded by

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

PowerShell Basics for System Administration

Uploaded by

mohamedtraka321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Alexandria University - Faculty of Science

Course: Operating Systems


Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
Windows PowerShell

Windows PowerShell is a powerful command-line shell and scripting language


developed by Microsoft. It's designed primarily for system administrators and power
users to automate tasks and manage configurations across Windows operating
systems. PowerShell allows users to perform administrative tasks via cmdlets
(pronounced "command-lets"), which are small, self-contained scripts or commands that
perform a specific function.

One of the key features of PowerShell is its ability to work with objects rather than just
text. This object-oriented approach allows for more flexibility and easier manipulation of
data. PowerShell also integrates with the .NET framework, enabling access to a wide
range of functions and libraries.

In addition to its command-line interface, PowerShell includes a scripting language that


supports variables, loops, conditionals, functions, and error handling, making it suitable
for writing complex automation scripts and tools.

Overall, Windows PowerShell is a versatile tool for system administration, automation,


and management tasks in Windows environments.

1. PowerShell Scripting Basics:

1.1 Launching the PowerShell:


PowerShell offers both a command-line option and an integrated scripting environment
(ISE):

- To launch the PowerShell command line, type [Link] in the Windows


Start menu.

- To launch the PowerShell ISE, type powershell_ise.exe in the Start menu.


Using the PowerShell ISE is the preferred way to work with the scripting
language because it provides syntax highlighting, auto-filling of commands and
other automation features that simplify script development and testing.
Alexandria University - Faculty of Science
Course: Operating Systems
Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
1.2 Preparing to Run PowerShell Scripts:

PowerShell scripts are stored in.ps1 files. You cannot run a script by simply double-
clicking a file; this design helps avoid accidental harm to your systems. Instead, to
execute a script, right-click it and choose Run with PowerShell.

In addition, there is a policy that restricts script execution. You can check this policy by
running the Get-ExecutionPolicy command in PowerShell.

You will get one of the following values:

Restricted — No scripts are allowed. This is the default setting, so you will see it the
first time you run the command.

AllSigned — You can run scripts signed by a trusted developer. Before executing, a
script will ask you to confirm that you want to run it.

RemoteSigned — You can run your own scripts or scripts signed by a trusted
developer.

Unrestricted — You can run any script you want.

To start working with PowerShell, you’ll need to change the policy setting from
Restricted to RemoteSigned using the Set-ExecutionPolicy RemoteSigned command.

1.3 PowerShell Cmdlets:


A cmdlet is a PowerShell command with a predefined function, similar to an operator in
a programming language.

A cmdlet always consists of a verb (or a word that functions as a verb) and a noun,
separated with a hyphen (the “verb-noun” rule).
Alexandria University - Faculty of Science
Course: Operating Systems
Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
For example, some of the verbs include:

Get — To get something


Set — To define something
Start — To run something
Stop — To stop something that is running
Out — To output something
New — To create something (“new” is not a verb, of course, but it functions as one)

For practice, try executing the following cmdlets:

Get-Process — Shows the processes currently running on your computer.


Get-Service — Shows the list of services with their status.
Get-Content — Shows the content of the file you specify (for example, Get-Content
C:\Windows\System32\drivers\etc\hosts).

You can list all cmdlets by executing the Get Help -Category cmdlet, Or to find a
specific command Get-Help Get-Process -Examples

You can also create your own custom cmdlets. Each cmdlet has several parameters
that customize what it does. The PowerShell ISE will automatically suggest all valid
parameters and their types after you type a cmdlet and a hyphen (-)

For example, the following cmdlet shows all services whose names start with “W”: Get-
Service -Name W*

You can also use aliases, which are shortened cmdlet names. For instance, instead of
Get-Help you can use just Help. Try running the following two commands and see
whether you get the same result:

Start-Process notepad
start notepad
Alexandria University - Faculty of Science
Course: Operating Systems
Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
Similarly, to stop this process, you can use either of the following commands:

Stop-Process -Name notepad


spps -Name notepad

To see all aliases, execute the Get-Alias cmdlet.

1.4 Comments:
Leaving comments in a script will help you better understand what the script does.
A string comment is a single line that starts with a number sign (#); block comments
spread across multiple lines, starting and ending with number signs and angle brackets:

<# a block command #>

1.5 Pipes:
A pipe passes data from one cmdlet to another. I used a pipe earlier to get all properties
of an object.

For example, if you execute the following script, you’ll get all services sorted by their
status:

Get-Service | Sort-Object -property Status

You can also use a pipe to output text to a file using a script like the following:

"Hello, World!" | Out-File C:\ps\[Link]

You can use multiple pipes. For instance, the following script lists all services, with the
first pipe excluding stopped services and the second pipe limiting the list to display
names only:

Get-Service | WHERE {$_.status -eq "Running"} | SELECT displayname


# “$_.” defines the current element in the pipe.
Alexandria University - Faculty of Science
Course: Operating Systems
Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
[Link] System Management Tasks Using PowerShell:
2.1 Viewing Objects in a Directory:

2.2 Creating Files and Folders:


Alexandria University - Faculty of Science
Course: Operating Systems
Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
2.3 Deleting Files and Folder:

2.4 Copying Files and Folders:


Alexandria University - Faculty of Science
Course: Operating Systems
Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
2.5 Moving Files and Directories:

2.6 Renaming Files:

Common questions

Powered by AI

Cmdlets in PowerShell follow a 'verb-noun' naming convention, which consists of a verb and a noun separated by a hyphen. This naming structure describes the action and the target object, making commands intuitive. Examples include Get-Process to retrieve currently running processes, Set-Service to configure service properties, and Start-Job to initiate a background job. This consistency helps users quickly understand the purpose of a cmdlet .

PowerShell's object-oriented approach allows users to manipulate objects rather than just text, which provides more flexibility and ease when handling data. This feature enables access to underlying data structures and attributes directly through objects, rather than parsing text outputs, resulting in more efficient and error-free scripts. Integrating with the .NET framework further expands its functionality by giving access to a rich library of methods and properties .

PowerShell provides several cmdlets for file system management, allowing users to perform various tasks such as viewing objects in a directory, creating files and folders, deleting, copying, moving, and renaming them. For instance, Get-ChildItem can be used to list files and directories; New-Item creates files or folders; Remove-Item deletes them; Copy-Item copies files or directories to a new location; Move-Item relocates them; and Rename-Item changes their names. These cmdlets offer robust tools for managing and organizing file systems programmatically .

PowerShell's integration with the .NET framework provides access to a large array of classes, methods, and properties available in .NET libraries, significantly enhancing the scripting capabilities beyond typical command-line interfaces. This integration allows scripts to leverage .NET's complex data types and operations, enabling the development of sophisticated tools and automations with rich functionalities. It also allows the manipulation of complex data structures and seamless interoperability within the Windows environment .

Pipes in PowerShell allow the output of one cmdlet to be the input for another, effectively chaining commands and creating efficient data processing pipelines. This enables complex operations to be executed seamlessly. For example, the script Get-Service | Sort-Object -property Status first retrieves all services, then sorts them by their status property, showcasing the use of pipes to connect cmdlets and streamline command execution .

The PowerShell ISE provides several advantages over the command-line interface, including syntax highlighting, which enhances code readability. It offers auto-completion and parameter suggestion features, which simplify script writing and reduce syntax errors. With debugging tools such as breakpoints and step execution, the ISE facilitates error identification and correction, making it an ideal choice for developing and testing complex scripts .

Including comments in PowerShell scripts is crucial for enhancing code readability and maintainability, allowing users to understand the script's logic and purpose. Single-line comments start with a hash symbol (#), while block comments are enclosed within <# and #>. These annotations help other developers or future users understand the functionality of the code, facilitating changes and debugging processes .

Aliases in PowerShell are shortcuts for cmdlets, allowing users to execute commands with less typing. For instance, the alias 'gci' can be used in place of 'Get-ChildItem'. This feature increases efficiency by speeding up command entry and reducing typing errors. Aliases are particularly useful for commonly used cmdlets and can be customized to fit users' preferences or needs, enhancing productivity in script management .

Execution policies in PowerShell determine which scripts can run on a system, providing a security measure to prevent unauthorized scripts from executing. The policy options include Restricted (default, no scripts allowed), AllSigned (only signed scripts run), RemoteSigned (local scripts run, remote ones need signing), and Unrestricted (all scripts run). To change an execution policy from Restricted to RemoteSigned, the command Set-ExecutionPolicy RemoteSigned must be executed in PowerShell .

PowerShell script execution poses security risks such as unapproved or malicious scripts running on a system. To mitigate these risks, PowerShell implements execution policies that control script permissions, such as RemoteSigned or AllSigned, which require scripts to be signed by a trusted authority. Administrators can also use strict permissions and auditing to monitor script activities. Regular updates and security patches, along with user education on script signing and policy configuration, further reduce vulnerabilities .

You might also like