Windows CMD & PowerShell Scripting - The Ultimate Guide

Windows CMD & PowerShell Scripting - The Ultimate Guide

Introduction

Windows Command Prompt (CMD) and PowerShell are two of the most powerful command-line tools used by IT professionals, developers, and system administrators to automate tasks, manage systems, and execute commands efficiently. CMD has been a part of Windows for decades, offering basic command-line functionality, while PowerShell is a more advanced and robust scripting language designed for automation, system administration, and task scheduling.

This comprehensive guide explores CMD and PowerShell scripting, their capabilities, differences, and real-world use cases to help you become proficient in command-line automation. Additionally, we will dive into ethical hacking and scripting, showcasing how cybersecurity professionals leverage automation and scripting languages to perform penetration testing and exploit development.

Understanding Windows Command Prompt (CMD)

CMD, also known as Command Prompt, is a legacy command-line interpreter that allows users to interact with the Windows operating system through text-based commands. It is primarily used for file manipulation, network troubleshooting, and basic system tasks.

Although CMD is not as powerful as PowerShell, it is lightweight and remains widely used for executing quick administrative tasks.

Commonly Used CMD Commands

The Command Prompt supports a wide range of commands that help in managing files, processes, and network settings. Some essential commands include:

  • dir - Displays the contents of a directory.
  • cd - Changes the current working directory.
  • md or mkdir - Creates a new directory.
  • rd or rmdir - Deletes a directory.
  • copy - Copies files from one location to another.
  • move - Moves files to a different location.
  • del - Deletes one or more files.
  • ren or rename - Renames a file.
  • cls - Clears the command prompt screen.
  • tasklist - Displays a list of running processes.
  • taskkill - Terminates a specified process.
  • shutdown - Shuts down, restarts, or logs off a computer.
  • ipconfig - Displays IP configuration details of the system.
  • ping - Tests network connectivity to another device.
  • netstat - Displays network connections, routing tables, and interface statistics.

CMD Scripting with Batch Files

CMD allows users to create batch files (.bat) to execute a series of commands automatically. These scripts are helpful in automating repetitive tasks.

Example 1: Automated System Cleanup (cleanup.bat)

@echo off
echo Cleaning system temporary files...
del /s /q C:\Temp\*.*
del /s /q C:\Windows\Temp\*.*
echo Cleanup completed successfully!
pause        

This script deletes temporary files to free up system space.

Example 2: Automated Backup (backup.bat)

@echo off
xcopy C:\Users\YourUsername\Documents D:\Backup /s /d /y
echo Backup completed successfully!
pause        

This script copies all modified files from the Documents folder to a backup drive.

Example 3: Checking Network Connectivity (pingtest.bat)

@echo off
echo Checking network connection...
ping google.com -n 4
echo Network test completed.
pause        

This script pings Google's servers to check internet connectivity.

PowerShell Scripting – The Modern Command-Line Interface

PowerShell is an advanced scripting language developed by Microsoft, built on the .NET framework. It provides extensive automation capabilities and offers deeper system control compared to CMD. PowerShell is both an interactive shell and a scripting language, making it ideal for task automation and configuration management.

Key Features of PowerShell

  • Works with objects rather than plain text, allowing for better data manipulation.
  • Supports cmdlets, which are specialized commands for different tasks.
  • Can access .NET libraries and Windows Management Instrumentation (WMI) for advanced system control.
  • Allows for remote administration using Windows Remote Management (WinRM).
  • Enables automation of system and network administration tasks.
  • Supports cross-platform functionality in newer versions (PowerShell Core).

Basic PowerShell Commands

PowerShell offers a rich set of commands, known as cmdlets, which perform specific functions. Some essential ones include:

  • Get-Help - Displays help information for any cmdlet.
  • Get-Process - Lists all running processes.
  • Stop-Process - Terminates a specified process.
  • Get-Service - Lists all system services.
  • Start-Service - Starts a specified service.
  • Stop-Service - Stops a specified service.
  • Get-ChildItem - Lists files and directories in a specified location.
  • Set-ExecutionPolicy - Changes the script execution policy.
  • Test-NetConnection - Checks network connectivity.

PowerShell Scripting with .ps1 Files

PowerShell scripts can be saved with a .ps1 extension and executed to automate complex tasks.

Example 1: Automated File Cleanup (cleanup.ps1)

Write-Host "Starting file cleanup..."
Get-ChildItem C:\Temp -Recurse | Remove-Item -Force
Write-Host "Temporary files deleted successfully."        

This script deletes all files in the C:\Temp directory.

Example 2: User Account Management (createuser.ps1)

$UserName = "NewUser"
$Password = ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force
New-LocalUser -Name $UserName -Password $Password -FullName "New User" -Description "New account created via script"
Write-Host "User account created successfully."        

This script creates a new user account with a predefined password.

Example 3: Checking Internet Connectivity (networktest.ps1)

Write-Host "Checking internet connectivity..."
Test-NetConnection google.com
Write-Host "Network test completed."        

This script checks internet connectivity using PowerShell.

Automating System Administration with PowerShell

PowerShell is widely used for automating administrative tasks, such as:

  • Managing Active Directory users and groups.
  • Automating software installations and updates.
  • Monitoring system performance and generating reports.
  • Managing network configurations and security settings.
  • Deploying scripts for cloud automation with Azure PowerShell.

CMD vs. PowerShell – Key Differences and Use Cases

CMD is suitable for basic tasks such as file management, system configuration, and network troubleshooting. It is simple and easy to use but lacks advanced scripting capabilities.

PowerShell, on the other hand, is designed for complex automation, system administration, and deep integration with Windows environments. It supports scripting, remote execution, and object-oriented programming, making it more powerful and flexible than CMD.

Conclusion

Mastering CMD and PowerShell scripting is essential for IT professionals, system administrators, and developers who want to increase productivity and efficiency. While CMD remains a fundamental tool for executing basic commands, PowerShell takes automation and system management to the next level with its advanced scripting capabilities.

Start experimenting with CMD and PowerShell scripts today to unlock the full potential of Windows automation!

#WindowsScripting #PowerShell #CMD #Automation #ITProfessional #Tech #SystemAdmin #Microsoft #CloudComputing #CyberSecurity #DevOps #Networking #ITSupport #Programming #Scripting #Windows #DataScience #EthicalHacking #PenTesting #Cybersecurity #Coding #LearnToCode #WindowsAutomation


To view or add a comment, sign in

More articles by Mohammed Kashif Malik

Others also viewed

Explore content categories