File and Folder Manipulation

File Manipulation

OperationPowerShell CommandDescription
Create a fileNew-Item -Path "path\filename.txt" -ItemType FileCreates a new file at the specified path
Copy a fileCopy-Item "source\file.txt" -Destination "dest\file.txt"Copies a file from source to destination
Move a fileMove-Item "source\file.txt" -Destination "dest\file.txt"Moves a file from source to destination
Rename a fileRename-Item "oldname.txt" -NewName "newname.txt"Renames a file
Delete a fileRemove-Item "path\filename.txt"Deletes the specified file
Read file contentGet-Content "path\filename.txt"Displays the content of a file
Write to a fileSet-Content "path\filename.txt" -Value "Text to write"Writes content to a file (overwrites existing content)
Append to a fileAdd-Content "path\filename.txt" -Value "Text to append"Appends content to the end of a file
Test if file existsTest-Path "path\filename.txt"Returns True if the file exists, False otherwise
Get file propertiesGet-ItemProperty "path\filename.txt"Retrieves the properties of a file
Set file attributesSet-ItemProperty "path\filename.txt" -Name Attributes -Value ReadOnlySets file attributes (e.g., ReadOnly, Hidden)
Get file hashGet-FileHash "path\filename.txt"Computes the hash of a file
Compare filesCompare-Object (Get-Content "file1.txt") (Get-Content "file2.txt")Compares the content of two files
Replace “path\filename.txt” with the actual path and filename you’re working with. Also, be cautious when using commands that modify or delete files, especially in production environments.

Folder Manipulation

OperationPowerShell CommandDescription
Create FolderNew-Item -Path "C:\Path\NewFolder" -ItemType DirectoryCreates a new folder at the specified path
Delete FolderRemove-Item -Path "C:\Path\FolderToDelete" -RecurseDeletes a folder and its contents
Copy FolderCopy-Item -Path "C:\SourceFolder" -Destination "C:\DestinationFolder" -RecurseCopies a folder and its contents to a new location
Move FolderMove-Item -Path "C:\SourceFolder" -Destination "C:\DestinationFolder"Moves a folder to a new location
Rename FolderRename-Item -Path "C:\OldFolderName" -NewName "NewFolderName"Renames a folder
Get Folder ContentsGet-ChildItem -Path "C:\FolderPath"Lists the contents of a folder
Get Folder Size`(Get-ChildItem -Path “C:\FolderPath” -RecurseMeasure-Object -Property Length -Sum).Sum`
Check if Folder ExistsTest-Path -Path "C:\FolderPath"Checks if a folder exists at the specified path
Get Folder PermissionsGet-Acl -Path "C:\FolderPath"Retrieves the access control list (ACL) for a folder
Set Folder PermissionsSet-Acl -Path "C:\FolderPath" -AclObject $AclSets the ACL for a folder (requires $Acl object)
Create JunctionNew-Item -ItemType Junction -Path "C:\JunctionPoint" -Target "C:\TargetFolder"Creates a junction point (symbolic link) to a folder
Replace the example paths (“C:\Path…”) with the actual paths you want to work with. Also, be cautious when using delete and move operations, as they can permanently affect your file system.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *