0% found this document useful (0 votes)
4 views

OS-Lab6

Uploaded by

mohamedtraka321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OS-Lab6

Uploaded by

mohamedtraka321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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 powershell.exe 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\test.txt

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
—--------------------------------------------------------------------------------------------------
2.File 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:

You might also like