Manipulating files
In this recipe, we will take a look at different cmdlets that help you manipulate files.
How to do it...
Let's explore different ways to manage files:
- Open PowerShell ISE as an administrator.
- Add the following script and run it:
#create file $timestamp = Get-Date -format "yyyy-MMM-dd-hhmmtt" $path = "C:\Temp\" $fileName = "$timestamp.txt" $fullPath = Join-Path $path $fileName New-Item -Path $path -Name $fileName -ItemType "File" #check if file exists Test-Path $fullPath #copy file $path = "C:\Temp\" $newFileName = $timestamp + "_2.txt" $fullPath2 = Join-Path $path $newFileName Copy-Item $fullPath $fullPath2 #move file $newFolder = "C:\Data" Move-Item $fullPath2 $newFolder #append to file Add-Content $fullPath "Additional Item" #show contents of file notepad $fullPath #merge file contents $newContent = Get-Content "C:\Temp\processes.txt" Add-Content $fullPath $newContent...