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

Lab 1 PowerShell (3)

Uploaded by

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

Lab 1 PowerShell (3)

Uploaded by

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

Name: Department of Intelligent Reg #:

Systems

Lab #1.PowerShell Basics


Introduction to PowerShell
PowerShell is a powerful command-line shell and scripting language designed to simplify and
automate system tasks. It is built on the .NET framework, making it a versatile tool for managing both
Windows and non-Windows systems. Unlike traditional command-line interfaces (CLI), PowerShell
uses cmdlets—specialized commands that follow a consistent Verb-Noun naming convention for
intuitive understanding. This format makes it easy to learn and remember the purpose of each
command, such as Get-Location (to retrieve the current directory) or New-Item (to create a new file
or folder).

One of the key advantages of PowerShell is that it is case-insensitive, meaning you can type
commands in any combination of uppercase or lowercase letters without affecting the outcome. To
make usage even more efficient, PowerShell includes aliases—shorter, alternative forms of cmdlets,
such as using cd in place of Set-Location. This allows users familiar with other command-line
interfaces, like Command Prompt or Linux's Bash shell, to transition smoothly into PowerShell.

Each cmdlet can be extended with additional parameters, enabling you to fine-tune commands and
control their output. For example, the -Recurse parameter in Get-ChildItem lets you list all
files in subdirectories, not just the current directory. This flexibility makes PowerShell a highly
customizable tool.

To start using PowerShell, simply click the Start menu, search for "PowerShell," and launch it.

Commands for Basic Navigation and File Management


Some basic commands used for navigation are shown in Table 1.
Table 1 Basic Commands for Navigation

Command Alias Description


Get-Location pwd Displays the current directory
Set-Location cd, chdir Changes the current directory
Get-ChildItem dir, ls, gci Lists files and directories
New-Item ni Creates a new file or directory
Remove-Item rm, del, rmdir Deletes a file or directory
Copy-Item cp, copy Copies files or directories
Move-Item mv, move Moves files or directories
Clear-Host cls, clear Clears the console

1. Get-Location (pwd)
 Description: Displays the current working directory (i.e., where you are located in the file
system).
 Alias: pwd (similar to the Unix/Linux command).
• Task 1 Display Your Current Directory
1. Open PowerShell.

Page 1
Name: Department of Intelligent Reg #:
Systems

2. Type Get-Location and hit Enter.


3. Observe the current path.
• Task 2 Use Alias pwd for Simplicity
1. In PowerShell, type pwd and hit Enter.
2. Compare the output with Get-Location.
• Task 3 Write your observations

2. Set-Location (cd, chdir)


 Description: Changes the current working directory to another specified location.
 Aliases: cd, chdir (same as in CMD or Linux).
• Task 1 Change to a New Directory
1. In PowerShell, type Set-Location C:\Windows
2. Run Get-Location to verify that you’ve moved to the C:\Windows directory.
• Task 2 Use Alias cd to Change Directory
1. Type cd C:\Users and hit Enter.
2. Run pwd to verify that you’ve moved to C:\Users.
• Task 3 Navigate One Level Up
1. While in a subdirectory (e.g., C:\Users), type Set-Location .. to move up one
level.
2. Use pwd to verify you are now in the parent directory.
• Task 4 Write your observations

3. Get-ChildItem (dir, ls, gci)


 Description: Lists all files and folders in the current directory.
 Aliases: dir (same as in CMD), ls (like in Linux), gci (shortened form).
• Task 1 List Contents of the Current Directory
1. In PowerShell, type Get-ChildItem and hit Enter.
2. Observe the list of files and folders.
• Task 2 Use Alias dir to List Contents

Page 2
Name: Department of Intelligent Reg #:
Systems

1. In PowerShell, type dir and hit Enter.


2. Compare the output with Get-ChildItem.
• Task 3 Use -Recurse to List All Subdirectories
1. Navigate to a directory with multiple subdirectories.
2. Type Get-ChildItem -Recurse to list all files and directories within the current
directory and its subdirectories.
• Task 4 List Hidden Files
1. In PowerShell, type Get-ChildItem -Force.
2. Observe that hidden files and directories (which start with a .) are now visible.
• Task 5 Write your observations

4. New-Item (ni)
 Description: Creates a new file or directory.
 Aliases: ni.
• Task 1 Create a New Directory
1. In PowerShell, type New-Item -Path C:\TestFolder -ItemType Directory.
2. Use Get-ChildItem C:\ to confirm that TestFolder was created.
• Task 2 Use Alias ni to Create a New Directory
1. In PowerShell, type ni -Path C:\TestFolder2 -ItemType Directory.
2. Verify the creation with Get-ChildItem C:\.
• Task 3 Create a New File in the Directory
1. In PowerShell, type New-Item -Path C:\TestFolder\MyFile.txt -
ItemType File.
2. Navigate to C:\TestFolder and run dir to check that MyFile.txt was created.
• Task 4 Write your observations

Page 3
Name: Department of Intelligent Reg #:
Systems

5. Remove-Item (rm, del, rmdir)


 Description: Deletes a file or directory.
 Aliases: rm (similar to Linux), del (similar to CMD), rmdir (removes directories).
• Task 1 Delete a File
1. In PowerShell, type Remove-Item C:\TestFolder\MyFile.txt.
2. Use Get-ChildItem C:\TestFolder to confirm that the file was deleted.
• Task 2 Use Alias rm to Delete a File
1. In PowerShell, type rm C:\TestFolder\MyFile.txt
2. Confirm that the file is no longer there.
• Task 3 Delete a Directory and Its Contents
1. In PowerShell, type Remove-Item -Path C:\TestFolder -Recurse.
2. Verify the deletion with Get-ChildItem C:\ to ensure TestFolder is gone.
• Task 4 Write your observations

6. Copy-Item (cp, copy)


 Description: Copies files or directories from one location to another.
 Aliases: cp (similar to Linux), copy (similar to CMD).
• Task 1 Copy a File
1. Create a new file using New-Item -Path C:\TestFolder\Sample.txt -
ItemType File.
2. In PowerShell, type Copy-Item C:\TestFolder\Sample.txt C:\
TestFolder\Copy_Sample.txt.
3. Run Get-ChildItem C:\TestFolder to see that Copy_Sample.txt was
created.
• Task 2 Use Alias cp to Copy a File
1. In PowerShell, type cp C:\TestFolder\Sample.txt C:\TestFolder\
Backup_Sample.txt.
2. Confirm that Backup_Sample.txt is created in C:\TestFolder.
• Task 3 Copy an Entire Directory
1. Create a subdirectory inside TestFolder using New-Item -Path C:\
TestFolder\SubFolder -ItemType Directory.
2. In PowerShell, type Copy-Item C:\TestFolder C:\BackupFolder -Recurse.
3. Verify that BackupFolder contains all files and subdirectories from TestFolder.
• Task 4 Write your observations

Page 4
Name: Department of Intelligent Reg #:
Systems

7. Move-Item (mv, move)


 Description: Moves files or directories from one location to another.
 Aliases: mv (similar to Linux), move (similar to CMD).
• Task 1 Move a File
1. In PowerShell, type Move-Item C:\TestFolder\Sample.txt C:\
TestFolder\Moved_Sample.txt.
2. Confirm that Sample.txt is renamed or moved to Moved_Sample.txt.
• Task 2 Use Alias mv to Move a File
1. In PowerShell, type mv C:\TestFolder\Moved_Sample.txt C:\
BackupFolder\Final_Sample.txt.
2. Confirm that Final_Sample.txt has been moved to C:\BackupFolder.
• Task 3 Move an Entire Directory
1. In PowerShell, type Move-Item C:\TestFolder C:\FinalFolder.
2. Confirm that the TestFolder directory has been moved to C:\FinalFolder.
• Task 4 Write your observations

8. Clear-Host (cls, clear)


 Description: Clears the content of the console window.
 Aliases: cls, clear (similar to CMD and Linux).
• Task 1 Clear the Console Screen
1. After running several commands, type Clear-Host in PowerShell.
2. The console screen should be cleared, leaving only a blank prompt.
• Task 2 Use Alias cls to Clear the Console
1. Type cls in PowerShell and hit Enter.
2. The console window should clear.
• Task 3 Write your observations

Page 5
Name: Department of Intelligent Reg #:
Systems

Page 6

You might also like