logo
Published on

Windows Command Shell - Essential cheat sheet

windows
Authors

Tip: If you type help from your windows command shell session, you will get a list of all available shell commands.

1. CMD - List all files and sub-directories in a directory

:: List contents of the current directory
dir

:: List contents of the specified directory
dir c:\Users\

2. CMD - Create a new directory

:: Basic usage
mkdir <new-directory-name>

:: Alternatively, use md
md new-directory

:: Create a new directory at a specified path
mkdir c:\\temp\test2

:: View built in help page
mkdir /?

3. CMD - Create or edit a file

notepad somenewfile.txt
  • Notepad will create a new file if it does not exist or open the existing file for editing.

4. CMD - Delete a file

del <file-path-or-name>

:: View built in help page
del/?

5. CMD - Remove a directory

:: Basic usage - this assumes the directory is empty
rmdir <path-to-directory>

:: See built-in help page for more options
rmdir /?

5.1 CMD - Remove a directory and all its contents

rmdir /s /q <directory-name>

6. CMD - View file contents

:: Print contents of a file to console output
type <filename>

:: View built-in help page
type /?

7. CMD - Set or display environment variables

:: Example: Add python to Windows path
SET path=%path%;c:\python27

:: View all environment variables
SET

:: View a specific environment variable e.g. PYTHONPATH
SET PYTHONPATH

:: View build in help page
SET /?

8. Copy, Move or Rename Files

:: Copy file to a new directory
copy <source-file-path> <destination-file-path>

:: View built-in usage guide
copy /?


:: Copy all files and sub-directories to  a new location
> xcopy <source-directory> <destination-directory>

:: View built-in help page
> xcopy /?


:: Move file to a new destination
> MOVE <source-path> <destination-path>

# View help page
> MOVE /?

Note that you can use the move command to rename a file. For example move hello1.py hello.py will essentially rename hello1.py into hello.py

9. Searching text

  • Use findstr to search text using regular expressions or literal string comparisons. Example:
:: Search for the word 'hello' inside a file named hello.py
type hello.py | findstr /R "hello"


:: Search for all text files in the current directory
dir | findstr /R ".*.txt"


:: See built-in help page for more information
findstr /?

10. Command Aliasing

  • If you are already familiar with the linux bash shell, you might be used to using ls to list the contents of a directory.
  • You can create an alias on the windows command shell to achieve the same purpose. Exampple:
doskey ls=dir

:: ls will now display the contents of the current directory
ls

Note that the alias will only be usable from the current shell session. If you restart the shell, you will need to re-create the alias.

11. CMD - Batch file scripting

  • These additional commands are useful when writing batch files (i.e. files with the .bat extension)
  • This is a useful resource for learning more about Windows batch file scripting

11.1 CMD - For loops


:: Iterate through a range of numbers
for /l %%i in (1,1,10) do (
    echo %%i
)

:: Iterate through files in a directory
for %%f in (*.txt) do (
    echo %%f
)

:: Iterate through a list of items
for %%item in (item1 item2 item3) do (
    echo %%item
)

11.2 CMD - Conditional statements

:: Check if a file exists
if exist <file-path> (
    echo File exists
) else (
    echo File does not exist
)

:: Check if a directory exists
if exist <directory-path> (
    echo Directory exists
) else (
    echo Directory does not exist
)

:: Compare two values
if <value1> EQU <value2> (
    echo Values are equal
) else (
    echo Values are not equal
)

11.3. CMD - Functions

:: Define a function
:my_function
    echo Hello, World!
    exit /b 0

:: Call a function
call :my_function

11.4. CMD - Comments

:: This is a comment

REM This is also a comment

11.5. CMD - Built-in script variables

:: The name of the currently running script
%0

:: The first argument passed to the script
%1


:: The list of all arguments passed to the script
%*

:: The number of arguments passed to the script
%#

:: The current line number
%line%

12. CMD - Using variables

:: Set a variable
set myvar=hello

:: Print the value of a variable
echo %myvar%

:: Print the value of a variable using delayed expansion
setlocal enabledelayedexpansion
echo !myvar!

13. CMD - Using environment variables

:: Display the value of an environment variable
echo %variable_name%

:: Assign a value to an environment variable
set variable_name=value

:: View all environment variables
set

:: Combine environment variables in a command
echo %USERPROFILE%\Documents

:: Use environment variables in batch scripts
@echo off
setlocal
set MY_VARIABLE=Hello, World!
echo %MY_VARIABLE%
endlocal

14. CMD - Execution Instructions


:: Run a batch or powershell script
call <script-name>

:: Or simply
.\<script-name>

:: Run a program or command
start notepad.exe

:: Run a program with elevated privileges (as administrator)
runas /user:Administrator cmd

:: Run a command in the background
start /b <command>

:: Run a command and suppress all output
<command> > nul 2>&1

:: Run a command and redirect output to a file
<command> > output.txt 2> error.txt

15. CMD - Persmissions and Security


:: Display file and folder permissions
icacls <file-or-folder>

:: Grant full control to a user or group
icacls <file-or-folder> /grant User:F

:: Remove permissions for a user or group
icacls <file-or-folder> /remove User

:: View effective permissions for a user
icacls <file-or-folder> /save effective-permissions.txt

16. CMD - Networking


:: Check network connectivity with ping
ping google.com

:: Display network configuration
ipconfig /all

:: Manage network connections
netsh interface show interface


:: View built-in help page
netsh /?

17. CMD - System Information


:: Display system information
systeminfo

:: Display system uptime
systeminfo | find "System Boot Time"

:: View built-in help page
systeminfo /?

18. CMD - System Management


:: Shutdown the system
shutdown /s

:: Restart the system
shutdown /r

:: View built-in help page
shutdown /?

19. CMD - User Management


:: Display information about the current user
whoami

:: Display information about all users
net user

:: Create a new user
net user <username> <password> /add

:: Delete a user
net user <username> /delete

:: View built-in help page
net /?

20. CMD - File Manipulation


:: Copy a file to another location
copy <source-file> <destination>

:: Move or rename a file
move <source> <destination>
rename <current-name> <new-name>

:: Delete a file
del <file-name>

:: View the contents of a text file
type <file-name>

:: View contents of a large file page by page
more <file-name>

:: Display file and folder attributes
attrib <file-or-folder>

:: Find files by their attributes
dir /a:<attributes>

:: Find and replace text in a file
findstr /i /c:"search-text" <file-name> > output.txt

:: Compare two text files
fc <file1> <file2>



References