Skip to main content

🗃️ PowerShell and file manipulation

  • Solo, at your own pace

🎯 Learning Objective​

  • Manipulate files and directories via PowerShell
  • Automate simple tasks with scripts
  • Use relative and absolute paths
  • Search for files and textual content in a directory tree
  • Display and modify file permissions

📜 Statement​

🔧 Context​

A system administrator assigns you a series of tasks to perform via PowerShell. These tasks could be automated in a small script. You need to create a working environment, manipulate files and directories, all using PowerShell.

📁 Step 1 – Prepare an environment​

  1. Open PowerShell in a working folder of your choice.
  2. Create the following structure with PowerShell:
project/
├── notes/
├── archives/
└── temp/
  1. In the notes folder, create 3 text files named course1.txt, course2.txt, course3.txt. Each of these files must contain a different sentence, including the word important in one of them. Insert the content directly into the files with PowerShell.
  2. In the temp folder, create a text file named temp.txt with the following content:
    This file is temporary.

Useful commands:

  • New-Item to create files and folders
  • Set-Content to write content into files
  • Get-ChildItem to list files and folders

📂 Step 2 – Searches and paths​

  1. Search for all .txt files in the project folder, including subfolders.
  2. Search for the word important in all text files.
  3. Display the absolute path of each .txt file.

Useful commands:

  • Get-ChildItem -Recurse to list files recursively
  • Select-String to search for text in files
  • Resolve-Path to get the absolute path of files

🔄 Step 3 – Automation and simple wrapping​

  1. Write a PowerShell script named manage.ps1 that performs the following operations:

    • Copies all files from notes to archives

    • Deletes the content of the temp folder if it exists (without deleting temp itself)

    • Renames the copied files in archives by adding _backup before .txt

      • Example: course1.txt → course1_backup.txt

Useful commands:

  • Test-Path to check the existence of a folder or file
  • Copy-Item to copy files
  • Remove-Item to delete files or folders
  • Rename-Item to rename files

Run your script multiple times to ensure it is idempotent (doesn't break if rerun multiple times).

🧪 Step 4 – Automatic verification​

Add to your script a command that:

  • Counts and displays the number of files in archives
  • Displays a message "Success: all files have been backed up." if this number is 3

🔐 Step 5 – NTFS Permissions (access rights)​

  1. In the notes/ folder, remove the current user's read permission on one of the files. This must be done using PowerShell.

  2. Try to read this file with Get-Content. What happens?

  3. Then restore the permission with PowerShell.

  4. Temporarily give the entire archives/ folder read-write permission only (no deletion).

  5. Then try to delete a file from archives/ with Remove-Item. What do you observe?