In this recipe, we will look at different cmdlets that help to manipulate files.
Let's explore different ways to manage files.
Open PowerShell ISE. Go to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
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" notepad $fullpath #merge file contents $newcontent = Get-Content "C:\Temp\processes.txt" Add-Content $fullpath $newcontent notepad $fullpath #delete file Remove-Item $fullpath