Windows PowerShell is a powerful scripting language that allows users to automate tasks and manage Windows-based systems. One of the most useful commands in Windows PowerShell is the Clear-Item command. This command is used to delete items from a specified location, such as a file, folder, or registry key. In this article, we’ll discuss the syntax, parameters, inputs, outputs, examples, and tips for using the Clear-Item command.
Syntax
The syntax for the Clear-Item command is as follows:
Clear-Item [-Path] Path [-Force] [-Include String] [-Exclude String] [-Filter String] [-Recurse] [-WhatIf] [-Confirm] [-Credential PSCredential] [-UseTransaction] [CommonParameters]
Parameters
The Clear-Item command has several parameters that can be used to customize the command’s behavior. These parameters are as follows:
Parameter | Description |
---|---|
-Path | The path of the item to delete. |
-Force | Forces the deletion of the item, even if it is read-only. |
-Include | Specifies the items to include in the operation. |
-Exclude | Specifies the items to exclude from the operation. |
-Filter | Specifies a filter to use when deleting items. |
-Recurse | Specifies that the command should delete all items in the specified path, including subfolders and their contents. |
-WhatIf | Displays what would happen if the command were to run. |
-Confirm | Prompts the user for confirmation before running the command. |
-Credential | Specifies the credentials to use when deleting items. |
-UseTransaction | Specifies that the command should use a transaction when deleting items. |
CommonParameters | This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. |
Inputs
The Clear-Item command accepts the following input types:
- String
- Object
- PSCredential
Outputs
The Clear-Item command does not generate any output.
Examples
Let’s look at some examples of how to use the Clear-Item command.
The following command deletes the file C:\test.txt:
Clear-Item -Path “C:\test.txt”
The following command deletes all files in the C:\temp folder, including subfolders and their contents:
Clear-Item -Path “C:\temp” -Recurse
The following command deletes all files in the C:\temp folder that have the extension .txt:
Clear-Item -Path “C:\temp” -Filter “*.txt” -Recurse
Tips
Here are some tips for using the Clear-Item command:
- Be sure to specify the full path of the item you want to delete.
- Use the -Force parameter if the item is read-only.
- Use the -Include and -Exclude parameters to specify which items to include or exclude from the operation.
- Use the -Filter parameter to specify a filter to use when deleting items.
- Use the -Recurse parameter to delete all items in the specified path, including subfolders and their contents.
- Use the -WhatIf parameter to display what would happen if the command were to run.
- Use the -Confirm parameter to prompt the user for confirmation before running the command.
- Use the -Credential parameter to specify the credentials to use when deleting items.
- Use the -UseTransaction parameter to use a transaction when deleting items.
By following these tips, you can ensure that the Clear-Item command is used correctly and safely.
Conclusion
The Clear-Item command is a powerful tool for deleting items from a specified location. It has several parameters that can be used to customize the command’s behavior, and it can be used to delete files, folders, and registry keys. By following the syntax, parameters, inputs, outputs, examples, and tips outlined in this article, you can ensure that the Clear-Item command is used correctly and safely.