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.

Practice

  1. You should be in the intro-to-powershell-lab directory if you’re not already.
  2. Use the New-Item command to create a new file called sample.txt.
  3. Use the Set-Content command to add some text to the sample.txt file, such as "Hello, PowerShell!" or whatever else you desire. You may need to look up how to accomplish this task.
  4. Use the Get-Content command to display the content of the sample.txt file. You may need to look up how to accomplish this task.
  5. Use the Copy-Item command to create a copy of the sample.txt file named sample-copy.txt. You may need to look up how to accomplish this task.
  6. Create a new directory called backup inside the intro-to-powershell-lab directory. You’ve done this before - what command should you use?
  7. Use the Move-Item command to move the sample-copy.txt file into the new backup directory. You may need to look up how to accomplish this task. Note that the Move-Item command cannot create directories, which is why you had to create the directory first.
  8. Use the Remove-Item command to delete the sample.txt.