If you’re looking to catalog the files and folders in a specific directory and its subfolders, a PowerShell script can be a powerful tool. In this tutorial, we’ll guide you through creating and using a PowerShell script to log detailed information about files and folders in your chosen directory.
Prerequisites
Before we dive in, make sure you have the following:
- Basic PowerShell Knowledge: A basic understanding of PowerShell commands will be helpful.
- Directory Location: Know the path to the directory you want to log information from.
- Text Editor: A text editor like Notepad is handy for creating and editing PowerShell scripts.
Step-by-Step Guide
Step 1: Open a Text Editor
- Open a text editor like Notepad. This is where we’ll create our PowerShell script.
Step 2: Copy the Script
- Copy the following PowerShell script and paste it into your text editor:
# 1. Define variables for folder location and log file
$folderLocation = "C:\YourFolderPath"
$logFile = "C:\YourLogFolderPath\log.txt"
# 2. Initialize the log file
"Type,Path,Name,Date Created,Last Modified" | Set-Content -Path $logFile
# 3. Recursively traverse the folder and subfolders
Get-ChildItem -Path $folderLocation -File -Recurse | ForEach-Object {
$path = $_.DirectoryName
$file = $_.Name
$created = $_.CreationTime
$modified = $_.LastWriteTime
# Check if the item is a file or folder
$type = "File"
if ($_ -is [System.IO.DirectoryInfo]) {
$type = "Folder"
}
# 4. Append file information to the log file
"$type,$path,$file,$created,$modified" | Add-Content -Path $logFile
}
Write-Host "Process completed. Log file created at $logFile"
Step 3: Customize Folder and Log File Paths
- Replace
$folderLocation
with the actual path of the folder you want to log information from. - Replace
$logFile
with the desired path for your log file.
Step 4: Save the Script
- Click on “File” in your text editor.
- Choose “Save As.”
- Set “Save as type” to “All Files.”
- Name the file with an
.ps1
extension, for example,LogInfo.ps1
. - Save the script to a location of your choice.
Step 5: Run the Script
- Open a PowerShell window.
- If necessary, set the execution policy to allow running PowerShell scripts by typing:
Set-ExecutionPolicy RemoteSigned
. - Navigate to the folder where you saved
LogInfo.ps1
using thecd
(Change Directory) command.
Step 6: Execute the Script
- Run the script by entering
.\LogInfo.ps1
and press Enter. - The script will process the specified folder, logging file, and folder information into the log file as specified.
Step 7: View the Log File
- Once the script completes, open the log file (specified in the script) using a text editor to see the collected data.
Important Notes
- Ensure you have permission to access the specified folder and create files in the specified log file location.
- Back up important data before running scripts that modify or create files.
- Always validate and understand scripts from untrusted sources to ensure they don’t contain harmful code.
With these steps, you’ve successfully created and used a PowerShell script to log file and folder information from a specific directory. This can be particularly useful for keeping track of your files or creating a record of folder contents for various purposes.