Your Search Bar For Shrewd Tips

How Do I Clear Powershell?


If you are wondering, “How do I clear PowerShell?”, the answer depends on what you want to clear. You may want to remove text from the screen, delete commands from your history, reset a variable, or erase saved command history from your computer. These actions are different: clearing the screen does not erase your commands, and clearing session history may not remove the history file saved by PowerShell. This guide explains the options, shows the commands to use, and helps you choose the right method for Windows PowerShell or PowerShell 7.

Clear the PowerShell Screen

To clear the text displayed in the PowerShell window, run:

Clear-Host

This removes the visible output from the console and gives you a fresh screen. It does not close PowerShell, undo commands, delete files, or clear your command history. It is a display-only action, so it is safe to use when you simply want to tidy up the window.

PowerShell also provides the short aliases cls and clear. For example:

cls

These aliases call the same screen-clearing function in a standard PowerShell session. If you are following a script or tutorial, Clear-Host is often the clearest option because its purpose is easy to recognize.

You can also use the keyboard shortcut Ctrl+L in many PowerShell setups that use PSReadLine. The shortcut may behave differently depending on the terminal, host application, or key bindings you have configured. If it does not work, type Clear-Host and press Enter.

Clear PowerShell in Windows Terminal

When PowerShell is running inside Windows Terminal, there can be a difference between clearing the PowerShell screen and clearing the terminal’s scrollback. Clear-Host clears the visible console area, but your terminal may still let you scroll up and view earlier output. That earlier text is part of the terminal display buffer, not PowerShell’s command history.

To remove the scrollback as well, use Windows Terminal’s menu or its configured clear-buffer shortcut, if available. You can review or change shortcuts in Windows Terminal settings under Actions. The exact option and shortcut can vary by version and personal configuration.

If you are using the classic Windows PowerShell console, the screen-clearing command generally clears the visible console without changing your history. In either host, remember that clearing the display is not a secure way to erase sensitive information. Output may remain in a transcript, terminal logs, screenshots, or other records.

Clear Commands from the Current PowerShell Session

To remove commands from the current session’s built-in history, run:

Clear-History

PowerShell keeps a session history that you can inspect with Get-History. After running Clear-History, commands are removed from that session history. You can clear history before closing a session or use this command when you want to prevent commands from appearing in the session’s history list.

By default, Clear-History clears the session’s history entries. To clear a particular entry, identify its ID with Get-History and use the -Id parameter. For example:

Clear-History -Id 3

Replace 3 with the ID you want to remove. You can also clear a range of entries using the command’s supported parameters. Check Get-Help Clear-History for details in your PowerShell version.

Keep in mind that session history and interactive command recall are not always the same thing. In many modern PowerShell installations, PSReadLine provides an additional history feature that saves commands between sessions. Clearing the session history alone may therefore leave commands available after you open a new window.

Delete Persistent PSReadLine History

PSReadLine often saves interactive command history to a file so that you can recall commands in later sessions. To find the path to that file, run:

(Get-PSReadLineOption).HistorySavePath

The location depends on your PowerShell version, operating system, and PSReadLine configuration. You can store the path in a variable to make it easier to work with:

$historyPath = (Get-PSReadLineOption).HistorySavePath

Before deleting or editing the file, close other PowerShell windows that could be writing to it. Then remove the history file with:

Remove-Item -Path $historyPath

PowerShell or PSReadLine may create a new history file the next time you use the shell. If you want to keep the file but remove its contents, you can empty it with:

Set-Content -Path $historyPath -Value ''

Be aware that an open session may save its history again when you continue using it or close it. For a more reliable cleanup, close PowerShell sessions first, clear or remove the history file, and then start a new session. You can use Test-Path $historyPath to check whether the file exists before taking action.

Clear Only a Specific Command from Saved History

If you want to remove one sensitive command rather than erase the entire PSReadLine history file, you can edit the file directly. First find its location using (Get-PSReadLineOption).HistorySavePath. Close all PowerShell windows, open the file in a text editor, locate the command, delete that line, and save the file.

This approach is useful when you want to preserve ordinary command history while removing a particular entry. However, it is not a guarantee that the command has been erased everywhere. The same text could be recorded in a transcript, a script, a log, a backup, or another application’s history. Avoid entering passwords, access tokens, or other secrets directly at the PowerShell prompt. Use a secure credential-handling method instead.

Clear a Variable in PowerShell

To remove a variable from the current session, use Remove-Variable. For example, to remove a variable named projectName, run:

Remove-Variable projectName

You can confirm that it has been removed by checking whether the variable still exists:

Test-Path Variable:\projectName

The command removes the variable from the current scope. If the variable was created in another scope, or has a global scope, you may need to specify that scope. For example:

Remove-Variable projectName -Scope Global

Use care when removing variables in a script or shared session. Some variables are used by PowerShell, modules, or your own scripts. Removing one can change how later commands behave. If you only want to reset a variable’s value but keep the variable, assign a new value instead, such as $projectName = $null.

Clear Functions, Aliases, or Other Session Items

PowerShell stores more than command output and variables. A session can also contain functions and aliases that were created or imported during use. To remove a function you created, use:

Remove-Item Function:\MyFunction

To remove a custom alias, use:

Remove-Item Alias:\myalias

Replace the example names with the function or alias you intend to remove. These commands affect the current PowerShell session; they do not necessarily remove definitions from your profile script or from a module. If an item returns when you start a new session, check your PowerShell profile or the script that creates it.

Avoid deleting all functions, aliases, variables, or providers just to “reset” PowerShell. Broad cleanup commands can remove tools that the shell or installed modules need. If your goal is a clean, temporary workspace, opening a new PowerShell window is usually simpler and safer.

Reset PowerShell by Closing and Reopening It

If you want to clear the screen, discard temporary variables, and start with a fresh session, close the PowerShell window and open a new one. This ends the current process and removes its session state, including variables and functions that were not saved elsewhere. It does not automatically delete persistent PSReadLine history, files, profiles, or transcripts.

Starting a new session is often the best solution when PowerShell behaves unexpectedly after many commands or when you are testing a script. If a profile is causing the same setup or error to return, you can start PowerShell without loading your profile by using the -NoProfile option from a suitable launcher or command prompt. This helps determine whether the issue comes from your profile rather than PowerShell itself.

Be cautious before closing a session that is running a long task. Check for active scripts, installations, or background jobs first. Closing the window can interrupt work that has not completed.

Common Mistakes When Clearing PowerShell

  • Assuming the screen command deletes history: Clear-Host, cls, and clear only clear the display.
  • Assuming session history is persistent history: Clear-History may not remove commands saved by PSReadLine.
  • Deleting a history file while PowerShell is open: an active session may write history back to the file.
  • Using broad removal commands: removing every variable, function, or alias can break the current session.
  • Treating a cleared console as secure deletion: transcripts, logs, backups, and terminal scrollback may retain information.

When in doubt, decide what you need to clear first. For a clean-looking window, use Clear-Host. For session history, use Clear-History. For saved PSReadLine history, locate and carefully clear or remove the history file after closing active sessions. For temporary session state, open a new PowerShell window.

Conclusion

The quickest answer to “How do I clear PowerShell?” is Clear-Host if you mean clearing the screen. Use Clear-History to remove entries from the current session’s history, and check the PSReadLine history file if you need to remove saved command history. Use Remove-Variable for a specific variable, or start a new session to reset temporary state. Since these commands do different jobs, choose the one that matches what you want to clear—and avoid entering sensitive information directly into command history in the first place.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →