File and Folder Manipulation
File Manipulation
Operation | PowerShell Command | Description |
---|---|---|
Create a file | New-Item -Path "path\filename.txt" -ItemType File | Creates a new file at the specified path |
Copy a file | Copy-Item "source\file.txt" -Destination "dest\file.txt" | Copies a file from source to destination |
Move a file | Move-Item "source\file.txt" -Destination "dest\file.txt" | Moves a file from source to destination |
Rename a file | Rename-Item "oldname.txt" -NewName "newname.txt" | Renames a file |
Delete a file | Remove-Item "path\filename.txt" | Deletes the specified file |
Read file content | Get-Content "path\filename.txt" | Displays the content of a file |
Write to a file | Set-Content "path\filename.txt" -Value "Text to write" | Writes content to a file (overwrites existing content) |
Append to a file | Add-Content "path\filename.txt" -Value "Text to append" | Appends content to the end of a file |
Test if file exists | Test-Path "path\filename.txt" | Returns True if the file exists, False otherwise |
Get file properties | Get-ItemProperty "path\filename.txt" | Retrieves the properties of a file |
Set file attributes | Set-ItemProperty "path\filename.txt" -Name Attributes -Value ReadOnly | Sets file attributes (e.g., ReadOnly, Hidden) |
Get file hash | Get-FileHash "path\filename.txt" | Computes the hash of a file |
Compare files | Compare-Object (Get-Content "file1.txt") (Get-Content "file2.txt") | Compares the content of two files |
Folder Manipulation
Operation | PowerShell Command | Description |
---|---|---|
Create Folder | New-Item -Path "C:\Path\NewFolder" -ItemType Directory | Creates a new folder at the specified path |
Delete Folder | Remove-Item -Path "C:\Path\FolderToDelete" -Recurse | Deletes a folder and its contents |
Copy Folder | Copy-Item -Path "C:\SourceFolder" -Destination "C:\DestinationFolder" -Recurse | Copies a folder and its contents to a new location |
Move Folder | Move-Item -Path "C:\SourceFolder" -Destination "C:\DestinationFolder" | Moves a folder to a new location |
Rename Folder | Rename-Item -Path "C:\OldFolderName" -NewName "NewFolderName" | Renames a folder |
Get Folder Contents | Get-ChildItem -Path "C:\FolderPath" | Lists the contents of a folder |
Get Folder Size | `(Get-ChildItem -Path “C:\FolderPath” -Recurse | Measure-Object -Property Length -Sum).Sum` |
Check if Folder Exists | Test-Path -Path "C:\FolderPath" | Checks if a folder exists at the specified path |
Get Folder Permissions | Get-Acl -Path "C:\FolderPath" | Retrieves the access control list (ACL) for a folder |
Set Folder Permissions | Set-Acl -Path "C:\FolderPath" -AclObject $Acl | Sets the ACL for a folder (requires $Acl object) |
Create Junction | New-Item -ItemType Junction -Path "C:\JunctionPoint" -Target "C:\TargetFolder" | Creates a junction point (symbolic link) to a folder |
Leave a Reply
Want to join the discussion?Feel free to contribute!