Skip to content

Windows Command Shell#

Useful Shell Commands#

  • Fast clone directory:
Batchfile
robocopy "%SRC%" "%DST%" /MIR

[!danger]- del will erase symlink's actual target, not just the symlink
To list all symlinks in a folder

Batchfile
@rem list all symlinks
@rem  /al: display files with Reparse Points attribute
@rem  /s: recurse
@rem  /b: leave out heading information/summary
dir /al/s/b "%FOLDER%"
Batchfile
@rem del: needed bc 'rmdir' cannot erase directory containing files
@rem  /f: force delete read-only files
@rem  /s: deletes specified files from all subdirectories
@rem  /q: quiet mode, suppress confirmation to delete with global wildcard
@rem rmdir:
@rem  /s: recursive erase of directory including files
@rem  /q: quiet mode, suppress confirmation to delete directory tree
del /f/s/q "%FOLDER%" > nul
rmdir /s/q "%FOLDER%"
  • Remove empty directories
Batchfile
robocopy "%FOLDER%" "%FOLDER%" /S/PURGE
Link Types To Files? To Folders? Across Volumes? May Not Exit? Command
Shortcut Yes Yes Yes Yes shortcut.exe -t target -a args
Symbolic link Yes Yes Yes Yes mklink /D LinkDir TargetDir or mklink LinkFile TargetFile
Hard link Yes No No No mklink /H LinkFile TargetFile
Junction (soft link) No Yes Yes on same computer Yes mklink /J LinkDir TargetDir
  • Symbolic Links/Directory Junctions: implemented using reparse points
  • Junction/Symlink: main difference is when looking at a remote server
  • Junctions are processed at the server
  • Directory symlinks are processed at the client
  • Hard Links: implemented with multiple file table entries pointing to same inode
  • if original filename is deleted, the hard link will still work bc it points directly to the data on disk
  • Reference here and here

Parameters#

  • %0: pathname of batch script itself
  • %1....%9: reference argument by number
  • %*: refers to all the arguments e.g. %1 %2 %3 %4 %5 ...%255
  • expansion modifiers
  • %~f1: expand %1 to a fully qualified path name e.g. c:\utils\MyFile.txt
  • %~d1: expand %1 to a drive letter only e.g. c:
  • %~p1: expand %1 to a path only including trailing \ e.g. \utils\
  • %~n1: expand %1 to a file Name without file extension or path - MyFile or if only a path is present, with no trailing backslash, the last folder in that path.
  • %~x1: expand %1 to a file eXtension only - .txt
  • %~s1: change the meaning of f, n, s and x to reference the Short 8.3 name (if it exists)
  • %~1: expand %1 removing any surrounding quotes "
  • %~a1: display the file attributes
  • %~t1: display the date/time
  • %~z1: display the file size
  • %~$PATH:1 search the PATH environment variable and expand %1 to the fully qualified name of the first match found
  • expansion modifiers can be combined
  • %~dp1 expand %1 to a drive letter and path only
  • %~sp1 expand %1 to a path shortened to 8.3 characters
  • %~nx2 expand %2 to a file name and extension only

Scripts#

  • cmd [options] "command" [parameters]: starts new shell in same window, optionally runing program/command/batch
  • environment is inherited but changes not persisted back
  • /c: runs command and auto terminates
  • /k: runs command and remain open
    • This is useful for testing, e.g. to examine variables
  • If /c or /k, remainder of command line processed as an immediate command in new shell
  • for multiple commands, surround with quotes and use command separator '&' or '&&'; e.g. cmd /c "foo.cmd && bar.cmd"
  • more usecases
  • start "title" [options] "command" [parameters]: start a program/command/batch in a new window
  • environment is inherited but changes not persisted back
  • [!info] behavior is different depending on context/command
    if command is shell command or batch file: processed with cmd.exe /K i.e. window remains open
    inside batch script, a start without /wait launches program and continues script execution

  • more usecases

  • call [parameters]: invoke a batch script or subroutine
  • environment is inherited but changes are persisted back
  • use setlocal and endlocal to keep variables in different files separate
  • more usecases
  • [!warning] invoking batch script from another without call or start terminates first script and allows second one to take over

  • pause,puase >nul: to pause execution until keypress (Reference)

Cheatsheet#

  • launching commands/scripts
Window OnExecute OnFinish Changes Example
new continue auto close non-persistent start "title" cmd /c bar.exe arg1 arg2
new continue keep open non-persistent start "title" cmd /k bar.exe arg1 arg2
new block auto close non-persistent start "title" /wait cmd /c foo.cmd arg1 arg2
new block keep open non-persistent start "title" /wait cmd /k foo.cmd arg1 arg2
same block auto close non-persistent cmd /c foo.bat arg1 arg2
same block keep open non-persistent cmd /k foo.bat arg1 arg2
same block keep open persistent call foo.bat arg1 arg2
  • escaping
Scenario Example
Run a program and pass a Filename parameter cmd /c write.exe c:\docs\sample.txt
Run a program and pass a Long Filename cmd /c write.exe "c:\sample documents\sample.txt"
Spaces in Program Path cmd /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""
Spaces in Program Path + parameters cmd /c ""c:\Program Files\demo.cmd"" Parameter1 Param2
Spaces in Program Path + parameters with spaces cmd /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
Launch Demo1 and then Launch Demo2 cmd /c ""demo1.cmd" & "demo2.cmd""
Scenario Example
`#!batch command > filename' Redirect command output to a file
`#!batch command >> filename' APPEND into a file
`#!batch command < filename' Type a text file and pass the text to command
`#!batch commandA | commandB' Pipe the output from commandA into commandB
`#!batch commandA & commandB' Run commandA and then run commandB
`#!batch commandA && commandB' Run commandA, if it succeeds then run commandB
`#!batch commandA || commandB' Run commandA, if it fails then run commandB
`#!batch commandA && commandB || commandC' If commandA succeeds run commandB, if it fails commandC

Misc Windows Stuff#

  • Change windows command shell default ScreenBufferSize & WindowSize (Reference)

  • modify registry key: HKEY_CURRENT_USER\Console\%SystemRoot%_System32_cmd.exe

  • Windows Update Bullshit fixes

  • Inspect windows file associations (Reference)

  • show associated file type for extension

    Batchfile
    C:\> assoc .txt
    .txt=txtfile
    

  • show associated actions for file type

    Batchfile
    C:\> ftype txtfile
    txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1