Powershell File Management

Path to current location

> Get-Location

List directory items (Get-ChildItem)

> Get-ChildItem
> dir
> dir | sort-object -Descending -Property lastwritetime

Create directory (New-Item)

> New-Item -name "Folder Name" -ItemType "Directory"
> New-Item -Path "c:\" -Name "logfiles" -ItemType "Directory"

Create file (New-Item)

> New-Item -Name "testfile1.txt" -ItemType "file" -Value "This is a text string."
> New-Item -Name "File Name.txt" -ItemType File
> New-Item -Path . -Name "testfile1.txt" -ItemType "file" -Value "This is a text string."

Delete file (Remove-Item)

> Remove-Item C:\Test\*.*
> Remove-Item * -Include *.doc -Exclude *.txt

Delete directory (Remove-Item)

> Remove-Item C:\Test\

Copy file (Copy-Item)

> Copy-Item "C:\CurrentFolder\test.txt" -Destination "C:\DestinationFolder"

Move file (Move-Item)

> Move-Item -Path C:\test.txt -Destination E:\Temp\tst.txt
> Move-Item -Path C:\Temp -Destination C:\Logs
> Move-Item -Path .\*.txt -Destination C:\Logs
> mi text2.txt folder_name

Read file content (get-content)

> Get-Content "C:\test.txt" -TotalCount 5

Add content to file (Add-content)

> Add-Content -Path "C:\test.txt" -Value "Text to add."