PowerShell Genie

Clear-Content

Use Clear-Content to empty a file without deleting it, leaving a zero-length file behind.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Clear-Content

Syntax

1PS> Clear-Content [-Path] <String[]> [-Force] [-Filter <String>] [-WhatIf] [<CommonParameters>]
See the command anatomy

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

Clear-Content [-Path] <String[]> [-Force] [-Filter <String>] [-WhatIf] [<CommonParameters>]
  1. Stage 1Clear-Content-Path -Force -Filter -WhatIf

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Clear-Content -Path 'C:\Temp\run.log'

    1PS> Clear-Content -Path 'C:\Temp\run.log'
    Show reference output

    Sample output · fictional data, shaped like the real thing

        Directory: C:\Temp
    
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a----         10/9/2026   2:14 PM              0 run.log

    Clear-Content -Path 'C:\Temp\*.log'

    1PS> Clear-Content -Path 'C:\Temp\*.log'
    Show reference output

    Sample output · fictional data, shaped like the real thing

        Directory: C:\Temp
    
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a----         10/9/2026   2:14 PM              0 app.log
    -a----         10/9/2026   2:14 PM              0 run.log
    -a----         10/9/2026   2:15 PM              0 debug.log

    Select Name, Length

    1PS> Clear-Content C:\Temp\app.log; Get-Item C:\Temp\app.log | Select-Object Name, Length
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Name     Length
    -------  ------
    app.log  0

    Parameters

    Parameter definitions:

    • -Path
    • -Force
    • -Filter
    • -WhatIf

    Clear-Content on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Clear-Content when you need to empty a file without deleting it, leaving a zero-length file behind. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: Access denied while emptying a file requires checking the file ACL and whether another process has an exclusive handle. Do not assume -Force grants missing permissions. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: Remove-Item. Clear-Content leaves the item in place but removes its content; Remove-Item deletes the item. Emptying a log and deleting a directory are different operations. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    Clear-Content reference on Microsoft Learn

    Recap

    Clear-Content Use Clear-Content to empty a file without deleting it, leaving a zero-length file behind.

    Next: Add-Content
    Genie guideClear-Content

    Use Clear-Content to empty a file without deleting it, leaving a zero-length file behind. The console below is pre-loaded — run it, then change a value and run it again.