> For the complete documentation index, see [llms.txt](https://x7331.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://x7331.gitbook.io/notes/windows-shells/command-prompt/basics.md).

# Basics

Located at `C:\Windows\System32\cmd.exe`.

## Basic Commands

```sh
# clear the screen
cls
# command history
doskey /history
```

<table><thead><tr><th width="205" align="right">Key/Command</th><th>Description</th></tr></thead><tbody><tr><td align="right"><code>doskey /history</code></td><td>Prints the session's command history.</td></tr><tr><td align="right"><code>page up</code></td><td>Places the first command in our session history to the prompt.</td></tr><tr><td align="right"><code>page down</code></td><td>Places the last command in history to the prompt.</td></tr><tr><td align="right"><code>⇧</code></td><td>View previously run commands.</td></tr><tr><td align="right"><code>⇩</code></td><td>View most recent commands run.</td></tr><tr><td align="right"><code>⇨</code></td><td>Types the previous command to prompt one character at a time.</td></tr><tr><td align="right"><code>⇦</code></td><td>N/A</td></tr><tr><td align="right"><code>F3</code></td><td>Will retype the entire previous entry to our prompt.</td></tr><tr><td align="right"><code>F5</code></td><td>Pressing F5 multiple times will allow you to cycle through previous commands.</td></tr><tr><td align="right"><code>F7</code></td><td>Opens an interactive list of previous commands.</td></tr><tr><td align="right"><code>F9</code></td><td>Enters a command to our prompt based on the number specified. The number corresponds to the commands place in our history.</td></tr></tbody></table>

## Interesting Directories

<table><thead><tr><th width="292">Location</th><th>Description</th></tr></thead><tbody><tr><td><code>C:\Windows\Temp</code></td><td>Contains temp system files accessible to all users on the system. All users have full access (rwx). Useful for dropping files as a low-privilege user on the system.</td></tr><tr><td><code>C:\Users\&#x3C;user>\AppData\Local\Temp</code></td><td>Similar to above, but accessible only to the user account that it is attached to. Useful when the attacker gains control of a local/domain joined user account.</td></tr><tr><td><code>C:\Users\Public</code></td><td>Allows any interactive logon account full access. Alternative to the global <code>\Temp</code> as it's less likely to be monitored for suspicious activity.</td></tr><tr><td><code>C:\Program Files</code></td><td>Contains all 64-bit applications installed on the system.</td></tr><tr><td><code>C:\Program Files (x86)</code></td><td>Contains all 32-bit applications installed on the system.</td></tr></tbody></table>

## Navigation

```sh
# list directory
dir
# list hidden files
dir /a:h
# current working directory
cd
chdir
# move to another directory
cd <PATH>
chdir <PATH>
# print the structure of current directory & subdirs
tree
# directories & files
tree /F
```

## Files & Dirs

### Directories

#### Basics

```sh
# create new dir
mkdir <name>
md <name>
# delete empty dir
rmdir <name>
rd <name>
# delete non-empty dir
rmdir /S <name>
rd /S <name>
# move dir
move <source> <dest>
```

#### Copy

`xcopy` has been deprecated for `robocopy`. `xcopy` resets any file attributes by default which can be useful from an attacker's perspective.

```sh
# copy dir
xcopy <source> <dest> <options>
# recursive (+empty dirs)
xcopy <source> <dest> /E 
# retain attributes
xcopy <source> <dest> /K
```

`robocopy` (*robust file copy*) is a combination of `copy`, `xcopy`, and `move`. It is made for large directories and drive syncing.&#x20;

```sh
# basic usage
robocopy <source> <dest>
```

### Files

#### View Content

```bash
# view contents
type <filename>
# view contents of all files at once
type *.txt
# read all files from all subdirectories
for /r %i in (*) do type "%i" >> all_files_content.txt
# view contents once-screen at a time, remove extra blank space
more <filename> /S
# we can also large-output commands to it
systeminfo | more
```

#### Create & Modify

```sh
# create a file
echo <text> > <filename>
fsutil file createNew <filename> 222
# rename a file
ren <filename> <newfilename>
rename <filename> <newfilename>
```

#### Delete

```sh
# delete a file
del <filename>
erase <filename>
# delete files based on a specific attribute
del /A:R *
```

#### Copy & Move

```sh
# copy a file (with validation)
copy <source> <dest> /V
# move a file
move <source> <dest>
```

## I/O

```shell
# redirect output to a file
systeminfo > <filename>
# append to a file
systeminfo >> <filename>
# pass input to a command
find /i "see" < <filename>
# pass output to a command
type <filename> | find /i "see"
```

#### Command Chaining

```sh
# just run one after another regardelss of status
<cmd1> & <cmd2>
# run cmd2 only if cmd1 succeeds
<cmd1> && <cmd2>
# run cmd2 only if cmd1 fails
<cmd1> || <cmd2>
```

## Resources

{% tabs %}
{% tab title="Documentation" %}
{% embed url="<https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands>" %}
{% endtab %}

{% tab title="SS64" %}
{% embed url="<https://ss64.com/nt/>" %}
{% endtab %}

{% tab title="Robocopy" %}
{% embed url="<https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy>" %}
{% endtab %}
{% endtabs %}
