Home » PowerShell Remove-Item

PowerShell Remove-Item

by Online Tutorials Library

PowerShell Remove-Item | PowerShell Remove file

The PowerShell Remove-Item cmdlet deletes one or more specified items. Because many providers support this cmdlet so that it can delete many different types of items, including files, folders, variables, registry keys, aliases, and functions.

Syntax

Parameters

-Path

The -Path parameter is used to specify a path of the items being removed. Wildcard characters are accepted.

-LiteralPath

The -LiteralPath parameter is used to specify a path to one or more locations. Its value is used exactly as it is typed. If the path includes the escape characters, enclose it in a single quotation mark. Single quotation mark tells the Windows PowerShell that it should not interpret any character as an escape sequence. There is no character in the cmdlet, which is interpreted as a wildcard.

-Confirm

The -Confirm parameter prompts a confirmation before running the cmdlet.

-Filter

The -Filter parameter specifies a filter to qualify the -Path parameter. The FileSystem provider is the only PowerShell provider that supports the uses of filters. This parameter is more efficient as the provider applies the filters when the cmdlet gets the object, rather than having PowerShell filters the object after they’re accessed.

-Recurse

The -Recurse parameter indicates that this cmdlet deletes the items in the given location and all the child items of the locations. When this parameter is used with the -Include parameter, it might not delete all subfolders or all child items.

-Force

The -Force parameter forces the cmdlet to delete those items which cannot be changed, such as hidden, read-only files, aliases, or variables. It cannot remove constant variables or aliases. Even using the -Force parameter, the cmdlet cannot override security permissions.

-Include

The items that this cmdlet includes in the operation are specified as a string array. The value of -Include parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:*, the wildcard character ‘*‘ is used to specify the contents of the C: directory.

-Exclude

The items that this cmdlet excludes in operation are specified as a string array. The value of -Exclude parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:*, the wildcard character ‘*‘ is used to specify the contents of the C: directory.

-WhatIf

The -WhatIf parameter displays what would happen if the cmdlet runs. The cmdlet does not execute.

-Stream

The -Stream parameter is a dynamic parameter that the FileSystem provider adds to Remove-Item cmdlet. It works only in the file system drives and introduced in Windows PowerShell 3.0.

Examples

Example1: Remove a specified file in current drive

PowerShell Remove-Item

The cmdlet, in this example, deletes a given s.txt file in the current drive.

Example 2:Delete the files that have a .txt file name extension

PowerShell Remove-Item

The cmdlet in this example deletes all of the files that have .txt extension from the D:PowerShell folder.

Example 3: Delete the document files in the current folder

PowerShell Remove-Item

The cmdlet in this example deletes all the files that have a .doc file name extension and a name that does not include 1 from the current folder. It uses the wildcard character ‘*‘ to specify the current. And it uses the -Include and -Exclude parameters to specify the files to delete.


You may also like