Intro to PowerShell Lab Creating, Deleting, and Modifying Files
Learning objective: By the end of this exercise, students will be able to create, delete, and modify files using PowerShell commands.
Commands to learn
Let’s dive into file operations. PowerShell provides a set of commands that allow you to create, delete, and modify files.
New-Item -ItemType File: Creates the specified file.Remove-Item: Deletes the specified file.Copy-Item: Copies the specified file or directory to the second specified file or directory.Move-Item: Moves the specified file or directory to the second specified file or directory.Get-Content: Displays the content of the specified file.Set-Content: Modifies the content of the specified file.
Practice
- You should be in the
intro-to-powershell-labdirectory if you’re not already. - Use the
New-Itemcommand to create a new file calledsample.txt. - Use the
Set-Contentcommand to add some text to thesample.txtfile, such as"Hello, PowerShell!"or whatever else you desire. You may need to look up how to accomplish this task. - Use the
Get-Contentcommand to display the content of thesample.txtfile. You may need to look up how to accomplish this task. - Use the
Copy-Itemcommand to create a copy of thesample.txtfile namedsample-copy.txt. You may need to look up how to accomplish this task. - Create a new directory called
backupinside theintro-to-powershell-labdirectory. You’ve done this before - what command should you use? - Use the
Move-Itemcommand to move thesample-copy.txtfile into the newbackupdirectory. You may need to look up how to accomplish this task. Note that theMove-Itemcommand cannot create directories, which is why you had to create the directory first. - Use the
Remove-Itemcommand to delete thesample.txt.