COMMAND-LINE INTERPRETER AND POWERSHELL ENVIRONMENT IN WINDOWS.
INTRODUCTION
Definition of a command interpreter: �A command interpreter is a program that accepts text commands from the user, processes them, and performs the corresponding operations in the operating system. It allows interaction with the system without a graphical interface.
Why do we need CLI (Command Line Interface)?
The CLI is used by administrators, developers, and advanced users to effectively interact with the operating system.
A BRIEF OVERVIEW OF THE WINDOWS COMMAND PROMPT (CMD.EXE)
History and evolution
The Windows command line ( cmd.exe ) has a long history and evolution:
A BRIEF OVERVIEW OF THE WINDOWS COMMAND PROMPT (CMD.EXE)
Main features of cmd.exe
While cmd.exe remains relevant, it lacks the capabilities of PowerShell , especially in automation and system management.
CMD
1. Working with files and directories
The command line allows you to perform basic operations with files and folders:
CMD
2. Network commands
Commands for diagnosing and managing network connections:
CMD
3. Managing users and processes
These commands allow you to effectively manage files, network, users, and processes in Windows using the command line.
CMD
BASIC LIMITATIONS OF CMD
BASIC LIMITATIONS OF CMD
WHY WAS AN ALTERNATIVE NEEDED?
All of these limitations led Microsoft to develop PowerShell – a powerful Windows administration tool that: �✅ Supports object-oriented design. �✅ Allows the use of .NET and WMI. �✅ Features a sophisticated scripting and modularity system. �✅ Provides tools for automating administration and working with remote systems.
PowerShell has become a powerful replacement for the outdated cmd.exe and is widely used by administrators and developers on modern Windows systems.
POWERSHELL : OVERVIEW AND DIFFERENCES FROM CMD
What is PowerShell ?
PowerShell is a powerful command shell and scripting language developed by Microsoft for task automation and Windows administration. Unlike the traditional command line ( cmd.exe ), PowerShell uses an object-oriented approach and allows for more efficient use of system resources.
🔹 Developed on the .NET Framework �🔹 Processes objects, not just text �🔹 Has a flexible system of modules and cmdlets �🔹 Allows you to automate complex administrative tasks �🔹 Supports working with remote systems
KEY DIFFERENCES BETWEEN POWERSHELL AND THE COMMAND PROMPT (CMD)
Characteristic | CMD | PowerShell |
Based on | Text processing | Object Model (.NET) |
Data type | Only lines | Objects that support methods and properties |
Teams | Simple commands (dir, copy) | Powerful Cmdlets (Get-Process, Get-Service) |
Scripts | Batch files (.bat, .cmd) | PowerShell scripts (.ps1) |
Automation | Limited | Full-fledged scripts with loops and logic |
Access to the system | Only basic functions | Access to Windows API, WMI, registry |
Working with remote systems | No built-in support | PowerShell Remoting (WS-Man, SSH) |
Scope of application | Local commands | Administration, automation |
EXAMPLE COMMANDS IN CMD AND POWERSHELL
PowerShell returns objects with detailed information that can be filtered and sorted.
📌 Example 2: Deleting files older than 30 days �🔹 CMD (no built-in solution, need a forfiles script )
forfiles /p C:\Logs /s /m *.* /d -30 /c " cmd /c del @file"
🔹 PowerShell (one line)
Get- ChildItem C:\Logs -Recurse | Where-Object {$_. LastWriteTime - lt (Get-Date). AddDays (-30)} | Remove-Item
PowerShell makes this more convenient and powerful.
POWERSHELL
PowerShell is a powerful tool for managing Windows, offering several advantages over the classic CMD command line. Key features include an object-oriented approach , file and process management , and task automation .
1. Object-oriented approach
Unlike cmd.exe , which only processes strings, PowerShell works with objects that have properties and methods. This makes it more flexible and convenient.
Example: Getting a list of processes
🔹 CMD
tasklist
The output will be text and the data will be difficult to process.
🔹 PowerShell
Get-Process
PowerShell returns objects that can be filtered, sorted, and processed. For example, sorting by CPU usage:
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
đź’ˇ Advantage : you can work with the results as objects, not just text.
POWERSHELL
2. Working with files and processes
PowerShell provides convenient cmdlets for managing files and processes.
Files and directories
Deleting files older than 30 days
Get- ChildItem C:\Logs -Recurse | Where-Object {$_. LastWriteTime - lt (Get-Date). AddDays (-30)} | Remove-Item
POWERSHELL
3. Task automation
PowerShell supports scripts (.ps1 scripts) , loops, conditions, and remote systems, making it a powerful automation tool.
Example: Automatic file backup
The script copies all files from C:\Source to D:\Backup , adding the date to the folder name:
$Date = Get-Date -Format "yyyy-MM-dd"
$BackupPath = "D:\Backup\$Date"
New-Item -ItemType Directory -Path
$BackupPath Copy-Item -Path "C:\Source\*" -Destination $BackupPath -Recurse
Automatically running scripts
scheduled execution of PowerShell scripts via Task Scheduler , which is convenient for administration.
POWERSHELL
PowerShell uses cmdlets —special commands that allow you to manage the system and automate tasks. The basic naming principle for commands is the verb -entity (e.g., Get-Process , Start -Service ).
1. Basic commands for working with PowerShell
1.1 Getting a list of all available commands
Get-Command
This command shows all available cmdlets , including built-in and installed modules.
1.2. Getting help on a command
Get-Help Get-Process
Used to get a description of a cmdlet , its parameters, and examples. To download the full documentation, run:
Update-Help
1.3. Getting help with examples
Get-Help Get-Service -Examples
POWERSHELL
2. Working with processes
2.1. Getting a list of running processes
Get-Process
2.2. Terminating a process (e.g. Notepad )
Stop-Process -Name notepad –Force
2.3. Sorting processes by processor load
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
3. Working with services
3.1. Getting a list of all services
Get-Service
3.2. Getting the status of a specific service (e.g. Print Spooler )
Get-Service -Name Spooler
3.3. Stopping the service
Stop-Service -Name Spooler –Force
3.4. Launching the service
Start-Service -Name Spooler
4. Examples of basic scripts
4.1. Script for monitoring processes (display processes consuming more than 50% CPU)
Get-Process | Where-Object { $_.CPU - gt 50 } | Select-Object ProcessName , CPU
4.2. Automatically delete files older than 30 days (PowerShell Copy Edit)
$Path = "C:\Logs"
Get- ChildItem $Path -Recurse | Where-Object { $_. LastWriteTime - lt (Get-Date). AddDays (-30) } | Remove-Item –Force
4.3. Creating a backup copy of powershell filesCopyEdit
$Date = Get-Date -Format " yyyy -MM-dd"
$Source = "C:\Data"
$Backup = "D:\Backup\$Date"
New-Item -ItemType Directory -Path $Backup
Copy-Item -Path "$Source\*" -Destination $Backup -Recurse
COMPARISON OF CMD AND POWERSHELL
PowerShell is a more powerful and functional shell than the classic CMD command line . The main differences are listed in the table:
Criterion | CMD (Command Prompt) | PowerShell |
Based on | Text processing | Object Model (.NET) |
Data type | Only lines | Objects with methods and properties |
Teams | Simple commands (dir, copy) | Cmdlets (Get-Process, Get-Service) |
Scripts | Batch files (.bat, .cmd) | PowerShell scripts (.ps1) |
Automation | Limited | Full-fledged scripts with loops and logic |
Access to the system | Basic Windows commands | Access to Windows APIs, WMI, .NET |
Working with remote systems | No built-in support | PowerShell Remoting (WS-Man, SSH) |
Working with files | Simple (copy, del) | Flexible (Copy-Item, Remove-Item) |
Working with processes | tasklist, taskkill | Get-Process, Stop-Process |
Working with services | Cannot be controlled directly | Get-Service, Start-Service, Stop-Service |
Editing the registry | Impossible | Get-Item, Set-Item for working with the registry |
Interactivity | Limited | A full-fledged object-oriented shell |
Module support | Absent | Yes (Import-Module, Find-Module) |
Interoperability with JSON/XML | Impossible | Built-in parsers (ConvertTo-Json, ConvertFrom-Json) |
Used for | Simple commands and diagnostics | Administration, automation, DevOps |
PART 1: WORKING WITH THE COMMAND PROMPT (CMD)
Task 1: Basic file system commands
cd C:\Users\Public
3. Create a new folder named TestFolder :
mkdir TestFolder
4. Go to the created folder:
cd TestFolder
5. Create an empty file test.txt:
echo > test.txt
6. Copy the file to the C:\Temp directory (create it if it doesn’t exist)
copy test.txt C:\Temp\
7. Delete the test.txt file:
del test.txt
8. Delete the TestFolder folder :
cd ..
rmdir TestFolder
PART 1: WORKING WITH THE COMMAND PROMPT (CMD)
Task 2: Working with processes and network commands
1. List running processes:
tasklist
2. Terminate the notepad.exe process (if it is running):
taskkill /IM notepad.exe /F
3. Check your connection to the Google website:
ping google.com
4. View the current network configuration:
ipconfig /all
PART 2: WORKING WITH POWERSHELL
Task 3: Basic PowerShell Commands
Get-Command
2. View help for the Get-Process command:
Get-Help Get-Process
3. Get a list of running processes:
Get-Process
4. Stop the notepad.exe process (if running):
Stop-Process -Name notepad –Force
5. List all services:
Get-Service
PART 2: WORKING WITH POWERSHELL
Task 4: Working with Files in PowerShell
1. Create a new folder C:\PS_Test:
New-Item -ItemType Directory -Path C:\PS_Test
2. Create a text file file1.txt in this folder:
New-Item -ItemType File -Path C:\PS_Test\file1.txt
3. Write the line "This is a test file" into the file:
Set-Content C:\PS_Test\file1.txt " This is a test file."
4. Read the contents of the file:
Get-Content C:\PS_Test\file1.txt
5. Copy the file to the C:\Backup folder (create it if necessary):
Copy-Item C:\PS_Test\file1.txt -Destination C:\Backup
6. Delete the original file:
Remove-Item C:\PS_Test\file1.txt
PART 2: WORKING WITH POWERSHELL
Task 5: Automate tasks with PowerShell
1. List all files in C:\Windows, sorted by size:
Get -ChildItem C:\Windows-Recurse | Sort-Object Length -Descending | Select-Object -First 10
2. Get a list of processes consuming more than 100 MB of memory:
Get-Process | Where-Object { $_. WorkingSet - gt 100MB }
3. Delete all files from the C:\Temp folder that were created more than 7 days ago:
Get- ChildItem C:\Temp -Recurse | Where-Object { $_. LastWriteTime - lt (Get-Date). AddDays (-7) } | Remove-Item -Force
4. Create and run a PowerShell script that outputs the text " Hello, PowerShell!" to the file C:\PS_Test\log.txt:
"Hello, PowerShell!" | Out-File C:\PS_Test\log.txt
THANK YOU FOR YOUR ATTENTION