Windows Command line

Megha PrabhakarMegha Prabhakar
5 min read

The Windows Command Line, also known as the Command Prompt or cmd, is a command-line interpreter for Windows operating systems. It allows users to interact with the operating system and execute commands, scripts, and programs.

Features

♦ Executing commands: Running commands to perform various tasks, such as file management, system configuration, and troubleshooting.

♦ Scripting: Creating batch files to automate repetitive tasks.

♦ Navigation: Navigating through directories and files using commands.

Basic Commands

Here are some basic commands for Windows command prompt:

  • cd: Change directory. Used to navigate through the file system.

  • dir: Display a list of files and subdirectories in the current directory.

  • mkdir: Create a new directory.

  • rmdir: Remove a directory.

  • copy: Copy a file or directory.

  • move: Move a file or directory.

  • del: Delete a file.

  • echo: Output text to the screen.

  • cls: Clear the command prompt screen.

Navigation

  • cd ~: Navigate to the user's home directory.

  • cd ..: Navigate to the parent directory.

  • cd \ : Navigate to the root directory.

File Management

  • type file.txt: Display the contents of a file.

  • copy file.txt newfile.txt: Copy a file.

  • move file.txt newlocation: Move a file.

  • del file.txt: Delete a file.

System Information

  • systeminfo: Display system information, such as the operating system version and hardware specifications.

  • tasklist: Display a list of running processes.

  • taskkill: Kill a running process.

Networking

  • ping: Test network connectivity to a host.

  • ipconfig: Display network configuration information.

Tips and Tricks

  • Use the Tab key to auto-complete file and directory names.

  • Use the Up and Down arrow keys to navigate through command history.

  • Use the doskey command to view and edit command history.

PowerShell

PowerShell is a task-based command-line shell and scripting language developed by Microsoft. It's designed for system administrators and power users to automate and manage Windows systems.

Features

  1. Task automation: Automating repetitive tasks and workflows.

  2. Scripting: Creating scripts to perform complex tasks.

  3. Integration: Integrating with other Microsoft products and technologies.

Common Uses

  1. System administration: Managing Windows systems, users, and groups.

  2. Automation: Automating tasks, such as file management and reporting.

  3. Scripting: Creating scripts to perform complex tasks.

PowerShell Concepts

  1. Cmdlets: Commands used in PowerShell.

  2. Modules: Collections of related cmdlets and functions.

  3. Pipelines: Chaining cmdlets together to perform complex tasks.

Basic PowerShell Commands

Here are some basic PowerShell commands to get you started:

Navigation and File Management

  • Get-Location: Display the current directory.

  • Set-Location: Change the current directory.

  • Get-ChildItem: List the files and subdirectories in the current directory.

  • New-Item: Create a new file or directory.

  • Remove-Item: Delete a file or directory.

  • Copy-Item: Copy a file or directory.

  • Move-Item: Move a file or directory.

Process Management

  • Get-Process: List the running processes.

  • Stop-Process: Stop a running process.

System Information

  • Get-Host: Display information about the PowerShell host.

  • Get-Date: Display the current date and time.

  • Get-ComputerInfo: Display information about the computer.

Help and Documentation

  • Get-Help: Display help information for a cmdlet.

  • Get-Command: List all available cmdlets.

Examples

#List the files in the current directory
Get-ChildItem

#Create a new directory
New-Item -ItemType Directory -Name MyDirectory

#Copy a file
Copy-Item -Path .\file.txt -Destination .\MyDirectory

#Get help for a cmdlet
Get-Help Get-ChildItem

PowerShell Scripts

PowerShell scripts are powerful tools for automating tasks and streamlining workflows. With a .ps1 extension, these scripts contain a series of PowerShell commands that can interact with systems, applications, and files.

Here's a step-by-step guide to writing simple PowerShell scripts:

Creating a PowerShell Script

  1. Open a text editor, such as Notepad or Visual Studio Code.

  2. Write your PowerShell commands in the text editor.

  3. Save the file with a .ps1 extension.

Basic Script Structure

A simple PowerShell script might look like this:

#Define a variable
$name = "John"

#Output a message
Write-Host "Hello, $name!"

Running a PowerShell Script

  1. Open PowerShell.

  2. Navigate to the directory where your script is located.

  3. Run the script by typing .\script_name.ps1.

Example Script

Here's an example script that prompts the user for their name and outputs a greeting:

#Prompt the user for their name
$name = Read-Host "What is your name?"

#Output a greeting
Write-Host "Hello, $name!"

Tips and Best Practices

  • Use comments: Comments are lines that start with the # symbol and are ignored by PowerShell. Use comments to explain what your script does and how it works.

  • Use variables: Variables are used to store values that can be used later in your script.

  • Use Write-Host: Write-Host is used to output messages to the console.

File Security

File security refers to the measures taken to protect files from unauthorized access, modification, or deletion.

File Security in Command Line

Here are some command-line tools and techniques for file security:

File Permissions

Setting permissions to determine what actions can be performed on files.

  • icacls: Set or view file permissions on Windows.

  • chmod: Change file permissions on Linux or macOS.

File Encryption

Encrypting files to protect their contents.

  • cipher: Encrypt or decrypt files on Windows.

  • openssl: Encrypt or decrypt files on Linux, macOS, or Windows.

File Access Control

Controlling access to files based on user identity.

  • attrib: Set or view file attributes on Windows.

  • chown: Change file ownership on Linux or macOS.

File Integrity

Monitoring file access and modifications.

  • fciv: Calculate file hashes on Windows.

  • md5sum or sha256sum: Calculate file hashes on Linux or macOS.

Examples

  • Encrypt a file using cipher on Windows: cipher /e file.txt

  • Change file permissions using chmod on Linux or macOS: chmod 600 file.txt

  • Calculate a file hash using md5sum on Linux or macOS: md5sum file.txt

These command-line tools and techniques can help you manage file security and protect your files from unauthorized access or modification.

Conclusion

The Windows Command Line and PowerShell provide powerful tools for managing files, executing commands, and automating tasks. Mastering these interfaces can improve productivity, streamline workflow, and enhance system control.

0
Subscribe to my newsletter

Read articles from Megha Prabhakar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Megha Prabhakar
Megha Prabhakar