Batch and PowerShell Comparison

Introduction

Batch (.bat) and PowerShell (.ps1) are both Windows scripting languages, but they are very different. Batch is old, limited, and mostly text-based. PowerShell is modern, object-based, powerful, and designed for automation and system administration.

1. Syntax Differences

REM Batch
echo Hello

# PowerShell
Write-Host "Hello"
    

2. Variables

REM Batch
set name=Kaloyan
echo %name%

# PowerShell
$name = "Kaloyan"
Write-Host $name
    

3. Loops

REM Batch
for %%i in (*) do echo %%i

# PowerShell
Get-ChildItem | ForEach-Object { $_.Name }
    

4. If Statements

REM Batch
if "%x%"=="10" echo match

# PowerShell
if ($x -eq 10) { Write-Host "match" }
    

5. File Reading

REM Batch
for /f "delims=" %%a in (file.txt) do echo %%a

# PowerShell
Get-Content file.txt
    

6. Getting System Info

REM Batch
wmic cpu get name

# PowerShell
Get-CimInstance Win32_Processor | Select-Object Name
    

7. Network Commands

REM Batch
ipconfig

# PowerShell
Get-NetIPConfiguration
    

8. Real Objects vs Text

PowerShell works with REAL objects, not strings.

# PowerShell example
(Get-Process)[0].CPU
    

9. Functions

REM Batch
:hello
echo Hello
goto :eof

# PowerShell
function Hello {
  Write-Host "Hello"
}
    

10. Error Handling

# PowerShell (Batch has nothing like this)
try {
  Remove-Item file.txt
} catch {
  Write-Host "Failed"
}
    

11. Script Execution Security

# Show policy
Get-ExecutionPolicy

# Allow scripts temporarily
Set-ExecutionPolicy -Scope Process Bypass
    

12. Performance

13. PowerShell Advantages

14. When to Use What

Summary