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:

  1. A menu display function
  2. Functions for each file management operation
  3. A main program loop that handles user input and calls appropriate functions
  4. Basic error handling for invalid user inputs
  5. 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.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *