To log command windows, you need to capture the text that appears in a Command Prompt or PowerShell window and save it to a file. The simplest method is to use the built-in redirection operator > or the Start-Transcript cmdlet in PowerShell. For an already-open window, you can also enable logging via the window's properties or use third-party tools like script on Linux or tee on macOS. This guide covers the most reliable, built-in ways to log command windows on Windows, Linux, and macOS, so you can keep a record of commands and output for troubleshooting, auditing, or documentation.

What You'll Need

  • A computer running Windows, Linux, or macOS.
  • Access to a command-line interface (Command Prompt, PowerShell, Terminal, or similar).
  • Basic familiarity with typing commands and navigating directories.
  • For some methods, administrative privileges may be required (e.g., to change system-wide settings).

Step-by-Step

  1. Choose your logging method based on your operating system and needs. For a one-off command, redirection is easiest. For an entire session, use a session logger like Start-Transcript (Windows PowerShell) or script (Linux/macOS).
  2. Windows Command Prompt (cmd.exe): Redirect output to a file. Open Command Prompt. To log the output of a single command, type: command > logfile.txt. For example, ipconfig > ipconfig_log.txt. This creates or overwrites logfile.txt with the command's output. To append instead of overwrite, use >>. To capture both standard output and errors, use > logfile.txt 2>&1.
  3. Windows Command Prompt: Log an entire session. You can start a session log by typing script? No, that's not built-in. Instead, use the doskey macro? Not reliable. The official way is to use PowerShell's Start-Transcript. Alternatively, you can enable logging via the Command Prompt window properties: right-click the title bar, choose Properties, go to the Options tab, and under "Command history", set "Buffer size" and check "Discard old duplicates"? That doesn't log to file. For a true session log in cmd, you can use powershell -Command "Start-Transcript -Path C:\path\to\log.txt" but that starts PowerShell. Better to use PowerShell directly.
  4. Windows PowerShell: Use Start-Transcript. Open PowerShell (run as administrator if needed). Type Start-Transcript -Path "C:\Logs\session.txt" (create the folder first if it doesn't exist). This begins recording all commands and output to the specified file. To stop, type Stop-Transcript. The transcript includes timestamps and metadata. Note: Start-Transcript does not capture output from external executables that write directly to the console? Actually it does capture most console output. It may not capture some low-level output, but it's the standard method.
  5. Linux/macOS Terminal: Use the script command. Open Terminal. Type script logfile.txt to start logging your session to logfile.txt. All subsequent commands and output will be recorded. To stop, type exit or press Ctrl+D. The log file will contain a header and footer with timestamps. For appending to an existing log, use script -a logfile.txt. On macOS, the syntax is similar.
  6. Linux/macOS: Redirect output for a single command. Use command > logfile.txt or command >> logfile.txt to append. To capture both stdout and stderr, use command > logfile.txt 2>&1. For piping to tee to see output and save it, use command | tee logfile.txt.
  7. Windows: Use PowerShell's Tee-Object for real-time logging. In PowerShell, you can pipe output to Tee-Object to display and save: command | Tee-Object -FilePath logfile.txt. This works for cmdlets and external commands.
  8. Verify the log file. After stopping logging, open the log file with a text editor to ensure it contains the expected content. Check for any missing output or errors.

Common Mistakes

  • Using the wrong redirection operator. > overwrites the file; >> appends. Accidentally overwriting a log file loses previous data.
  • Forgetting to capture errors. By default, only standard output is redirected. Use 2>&1 to include error messages.
  • Not specifying a full path. If you don't provide a path, the log file is created in the current working directory, which may be hard to find later.
  • Assuming Start-Transcript captures everything. It may not capture output from some native commands or applications that write directly to the console buffer. Test with your specific commands.
  • Forgetting to stop logging. In PowerShell, if you close the window without running Stop-Transcript, the transcript may be incomplete or locked. Always stop logging properly.
  • Using script without exiting properly. On Linux/macOS, if you kill the terminal without exiting script, the log file may be incomplete or contain control characters.

Tips

  • Use descriptive log file names with dates, e.g., session_2025-03-23.log, to avoid confusion.
  • For long-running sessions, consider rotating logs or using a logging tool that handles file sizes.
  • In PowerShell, you can set a default transcript path in your profile to automate logging.
  • On Linux, script can be combined with timing file to replay sessions using scriptreplay.
  • Always review logs for sensitive information (passwords, personal data) before sharing.

Troubleshooting

  • Log file is empty or missing output: Check that you used the correct redirection operator and that the command actually produced output. For Start-Transcript, ensure you have write permissions to the target folder.
  • Permission denied when creating log file: Run the command prompt or terminal as administrator (Windows) or use sudo (Linux/macOS) if writing to a protected location. Alternatively, choose a folder you have write access to, like your home directory.
  • Start-Transcript not capturing external command output: Some console applications bypass PowerShell's output stream. Try using cmd /c command > logfile.txt within PowerShell, or use Tee-Object.
  • script command not found on macOS: The script command is available by default on macOS. If missing, ensure your PATH is set correctly.
  • Log file contains strange characters: This can happen if the output includes ANSI escape codes. Use a viewer that interprets them or strip them with tools like sed.
  • Cannot stop logging: In PowerShell, if Stop-Transcript fails, close the window and check the file; it may still be usable. On Linux, press Ctrl+D or type exit.

Frequently Asked Questions

Can I log command windows without using redirection?

Yes, on Windows you can use PowerShell's Start-Transcript for a full session log. On Linux/macOS, the script command does the same. These capture everything displayed in the terminal.

Does redirecting output also capture error messages?

No, by default only standard output is redirected. To include errors, add 2>&1 to the command, e.g., command > log.txt 2>&1.

How do I log output from a command that runs continuously?

Use tee to display and save output in real time: command | tee log.txt. On Windows PowerShell, use Tee-Object. For long-running processes, consider using Start-Transcript and letting it run.

Is there a built-in way to log Command Prompt sessions on Windows?

Command Prompt itself does not have a built-in session logger. You can use PowerShell's Start-Transcript or redirect individual commands. Alternatively, use the script command if you have Windows Subsystem for Linux (WSL) installed.

Can I log command windows on a remote server?

Yes, if you have SSH access, you can run script on the remote Linux server, or use Start-Transcript in a remote PowerShell session. Ensure you have write permissions on the remote system.

What is the difference between > and >>?

> creates a new file or overwrites an existing one. >> appends to an existing file or creates a new one if it doesn't exist. Use >> to preserve previous logs.