Simple File Management Tool
Description:
This PowerShell script is designed for beginners to learn basic file management operations. It provides a menu-driven interface that allows users to perform common tasks such as creating directories, listing files, copying files, and deleting files. The script demonstrates the use of functions, user input handling, and basic PowerShell cmdlets for file system operations.
Full Script:
# Simple File Management Tool # Function to display the menu function Show-Menu { Clear-Host Write-Host "=== File Management Tool ===" Write-Host "1. Create a new directory" Write-Host "2. List files in a directory" Write-Host "3. Copy a file" Write-Host "4. Delete a file" Write-Host "5. Exit" } # Function to create a new directory function Create-NewDirectory { $dirName = Read-Host "Enter the name of the new directory" New-Item -Path $dirName -ItemType Directory Write-Host "Directory created successfully." } # Function to list files in a directory function List-Files { $dirPath = Read-Host "Enter the directory path" Get-ChildItem -Path $dirPath } # Function to copy a file function Copy-File { $sourcePath = Read-Host "Enter the source file path" $destPath = Read-Host "Enter the destination path" Copy-Item -Path $sourcePath -Destination $destPath Write-Host "File copied successfully." } # Function to delete a file function Delete-File { $filePath = Read-Host "Enter the file path to delete" Remove-Item -Path $filePath Write-Host "File deleted successfully." } # Main program loop do { Show-Menu $choice = Read-Host "Enter your choice (1-5)" switch ($choice) { "1" { Create-NewDirectory } "2" { List-Files } "3" { Copy-File } "4" { Delete-File } "5" { Write-Host "Exiting program..."; break } default { Write-Host "Invalid choice. Please try again." } } if ($choice -ne "5") { Read-Host "Press Enter to continue..." } } while ($choice -ne "5")
This script includes:
- A menu display function
- Functions for each file management operation
- A main program loop that handles user input and calls appropriate functions
- Basic error handling for invalid user inputs
- Use of PowerShell cmdlets like New-Item, Get-ChildItem, Copy-Item, and Remove-Item
The script provides a simple interface for users to perform basic file management tasks, making it suitable for beginners to understand PowerShell scripting concepts and file system operations.