8/22/2019 Powershell Delete File
Use PowerShell to Delete a Single File or Folder. Let’s start by running a simple command to delete a single file or folder. Make sure you are logged in to the server or PC with an account that. Remove characters from file names with PowerShell Posted on January 4, 2016 by admin As we all know, OneDrive does not accept files or folders containing certain characters, so here is a short guide on how to remove characters from file names with PowerShell.
-->
SyntaxDescription
The
Remove-Item cmdlet deletes one or more items.Because it is supported by many providers, it can delete many different types of items, includingfiles, folders, registry keys, variables, aliases, and functions.
Examples
Example 1: Delete files that have any file name extension
This command deletes all of the files that have names that include a dot (
. ) from the C:Test folder. Because the command specifies a dot, the command does not delete folders or files that haveno file name extension.
Example 2: Delete some of the document files in a folder
This command deletes from the current folder all files that have a
.doc file name extension and aname that does not include 1.
It uses the wildcard character ('*') to specify the contents of the current folder. It uses theInclude and Exclude parameters to specify the files to delete.
Example 3: Delete hidden, read-only files
This command deletes a file that is both hidden and read-only.
It uses the Path parameter to specify the file. It uses the Forceparameter to delete it. Without Force, you cannot delete read-only orhidden files.
Example 4: Delete files in subfolders recursively
This command deletes all of the CSV files in the current folder and all subfolders recursively.
Because the Recurse parameter in
Remove-Item has a known issue, the command in this exampleuses Get-ChildItem to get the desired files, and then uses the pipeline operator to pass them toRemove-Item .
In the
Get-ChildItem command, Path has a value of (* ), which represents the contents of thecurrent folder. It uses Include to specify the CSV file type, and it uses Recurse to makethe retrieval recursive. If you try to specify the file type the path, such as -Path *.csv , thecmdlet interprets the subject of the search to be a file that has no child items, and Recursefails.
Example 5: Delete subkeys recursively
This command deletes the 'OldApp' registry key and all its subkeys and values.It uses
Remove-Item to remove the key.The path is specified, but the optional parameter name (Path) is omitted.
The Recurse parameter deletes all of the contents of the 'OldApp' key recursively.If the key contains subkeys and you omit the Recurse parameter, you are prompted to confirm thatyou want to delete the contents of the key.
Example 6: Deleting files with special characters
The following example shows how to delete files that contain special characters like brackets orparentheses.
Example 7: Remove an alternate data stream
This example shows how to use the Stream dynamic parameter of the
Remove-Item cmdlet to delete analternate data stream. The stream parameter is introduced in Windows PowerShell 3.0.
The first command uses the Stream dynamic parameter of the
Get-Item cmdlet to get theZone.Identifier stream of the 'Copy-Script.ps1' file.
The second command uses the Stream dynamic parameter of the
Remove-Item cmdlet to remove theZone.Identifier stream of the file.
The third command uses the Stream dynamic parameter of the
Get-Item cmdlet to verify that theZone.Identifier stream is deleted.
Parameters
-Confirm
Prompts you for confirmation before running the cmdlet.
Note
This parameter is not supported by any providers installed with PowerShell.To impersonate another user, or elevate your credentials when running this cmdlet,use Invoke-Command.
Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The valueof this parameter qualifies the Path parameter. Enter a path element or pattern, such as
*.txt . Wildcard characters are permitted. The Exclude parameter is effective only when thecommand includes the contents of an item, such as C:Windows* , where the wildcard characterspecifies the contents of the C:Windows directory.
Specifies a filter to qualify the Path parameter. The FileSystemprovider is the only installed PowerShell provider that supports the use of filters. You can findthe syntax for the FileSystem filter language in about_Wildcards.Filters are more efficient than other parameters, because the provider applies them when the cmdletgets the objects rather than having PowerShell filter the objects after they are retrieved.
Forces the cmdlet to remove items that cannot otherwise be changed, such as hidden or read-onlyfiles or read-only aliases or variables. The cmdlet cannot remove constant aliases or variables.Implementation varies from provider to provider.For more information, see about_Providers.Even using the Force parameter, the cmdlet cannot override security restrictions.
Specifies, as a string array, an item or items that this cmdlet includes in the operation. The valueof this parameter qualifies the Path parameter. Enter a path element or pattern, such as
'*.txt' . Wildcard characters are permitted. The Include parameter is effective only when thecommand includes the contents of an item, such as C:Windows* , where the wildcard characterspecifies the contents of the C:Windows directory.
Specifies a path to one or more locations. The value of LiteralPath is used exactly as it istyped. No characters are interpreted as wildcards. If the path includes escape characters, encloseit in single quotation marks. Single quotation marks tell PowerShell not to interpret any charactersas escape sequences.
For more information, see about_Quoting_Rules.
Specifies a path of the items being removed.Wildcard characters are permitted.
Indicates that this cmdlet deletes the items in the specified locations and in all child items ofthe locations.
When it is used with the Include parameter, the Recurse parameter might not delete allsubfolders or all child items. This is a known issue.As a workaround, try piping results of the
Get-ChildItem -Recurse command to Remove-Item , asdescribed in 'Example 4' in this topic.
The Stream parameter is a dynamic parameter that the FileSystem provider adds to
Remove-Item .This parameter works only in file system drives.
You can use
Remove-Item to delete an alternative data stream.However, it is not the recommended way to eliminate security checks that block files that aredownloaded from the Internet.If you verify that a downloaded file is safe, use the Unblock-File cmdlet.
This parameter was introduced in Windows PowerShell 3.0.
Shows what would happen if the cmdlet runs.The cmdlet is not run.
Inputs
System.String
You can pipe a string that contains a path, but not a literal path, to this cmdlet.
Outputs
None
This cmdlet does not return any output.
Notes
The
Remove-Item cmdlet is designed to work with the data exposed by any provider. To list theproviders available in your session, type Get-PsProvider . For more information, seeabout_Providers.
Related Links
I want to delete the files which are in a particular folder using PowerShell commands, but when I use wonea
Remove-Item $DownloadFilePath* -recurs (where $DownloadFilePath carries the path of the folder) to delete files then it deletes the folder itself and the files which are in its parent folder.Please let me know the solution to just delete files inside the folder.
2,3311515 gold badges6565 silver badges123123 bronze badges
Hitendra RaghuwanshiHitendra Raghuwanshi
2 Answers
This should delete just the contents of Ansgar WiechersAnsgar Wiechers
$DownloadFilePath :
149k1515 gold badges140140 silver badges198198 bronze badges
And if you only want to remove files but no sub folders you can use:
DarkLite1DarkLite1
4,5411515 gold badges5757 silver badges108108 bronze badges
Not the answer you're looking for? Browse other questions tagged powershell or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |