Discover powerful PowerShell scripts to automate tasks, manage systems, and boost productivity. Learn to create, modify, and execute scripts for Windows administration, network management, and more. Find expert tips, best practices, and ready-to-use scripts for both beginners and advanced users. Enhance your IT skills with our comprehensive PowerShell script resources.

Tag Archive for: PowerShell Script

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.

Automating File Organization with PowerShell: A Simple Script for Busy Professionals

As our digital lives become increasingly complex, keeping our files organized can be a daunting task. Fortunately, PowerShell offers a powerful solution for automating file management tasks. In this post, we’ll explore a simple yet effective PowerShell script that can help you organize your files based on their extension.

The Problem: You have a folder full of various file types – documents, images, spreadsheets, and more. Manually sorting these files into appropriate subfolders is time-consuming and prone to errors.

The Solution: A PowerShell script that automatically categorizes files based on their extensions and moves them into corresponding subfolders.

Here’s the script:

# Set the path to the folder you want to organize
$sourceFolder = "C:\Users\YourUsername\Desktop\ToOrganize"

# Create a hashtable to map file extensions to folder names
$extensionMap = @{
    ".txt" = "TextFiles"
    ".doc" = "WordDocuments"
    ".docx" = "WordDocuments"
    ".xls" = "ExcelFiles"
    ".xlsx" = "ExcelFiles"
    ".pdf" = "PDFs"
    ".jpg" = "Images"
    ".png" = "Images"
    ".gif" = "Images"
}

# Get all files in the source folder
$files = Get-ChildItem -Path $sourceFolder -File

foreach ($file in $files) {
    $extension = $file.Extension.ToLower()
    
    if ($extensionMap.ContainsKey($extension)) {
        $destinationFolder = Join-Path -Path $sourceFolder -ChildPath $extensionMap[$extension]
        
        # Create the destination folder if it doesn't exist
        if (!(Test-Path -Path $destinationFolder)) {
            New-Item -ItemType Directory -Path $destinationFolder | Out-Null
        }
        
        # Move the file to the appropriate folder
        Move-Item -Path $file.FullName -Destination $destinationFolder
    }
}

Write-Host "File organization complete!"

How it works:

  1. We define the source folder where our unorganized files are located.
  2. A hashtable maps file extensions to folder names.
  3. The script gets all files in the source folder.
  4. For each file, it checks the extension and moves it to the corresponding subfolder.
  5. If the subfolder doesn’t exist, it’s created automatically.

To use this script:

  1. Copy the script into a new .ps1 file.
  2. Modify the $sourceFolder variable to point to your desired folder.
  3. Adjust the $extensionMap hashtable if you want to add or change file categories.
  4. Run the script in PowerShell.

This simple script can save you hours of manual file sorting. Feel free to customize it to fit your specific needs. For instance, you could add more file extensions or create more specific categorizations.

Remember, PowerShell is a powerful tool, and with great power comes great responsibility. Always ensure you understand what a script does before running it, especially when it involves moving files.

Happy scripting, and enjoy your newly organized files!