Powershell Snippets

Powershell Snippets

Read Time: 3 minutes

PowerShell is a powerful scripting language that can streamline your tasks and automate repetitive processes. In this comprehensive guide, we will delve into the world of PowerShell snippets, providing you with a collection of code blocks to enhance your productivity. Whether you’re a system administrator, developer, or IT professional, mastering these PowerShell snippets will empower you to accomplish more with less effort.

Introduction to PowerShell Snippets

Discover the concept of PowerShell snippets and how they can accelerate your scripting:

  • Understand the purpose of snippets and their role in PowerShell development.
  • Learn how to create, store, and organize your snippets for easy access.
  • Explore the benefits of using snippets for code reuse and increased efficiency.

Essential PowerShell Snippets

File Manipulation

Copy a file

Copy-Item -Path "C:\Path\to\source\file.txt" -Destination "C:\Path\to\destination\folder"

Move a file

Move-Item -Path "C:\Path\to\source\file.txt" -Destination "C:\Path\to\destination\folder"

Delete a file

Remove-Item -Path "C:\Path\to\file.txt" -Force

Rename a file

Rename-Item -Path "C:\Path\to\oldfile.txt" -NewName "newfile.txt"

Retrieve System Information

$computerInfo = Get-WmiObject -Class Win32_ComputerSystem
$operatingSystem = Get-WmiObject -Class Win32_OperatingSystem

$manufacturer = $computerInfo.Manufacturer
$model = $computerInfo.Model
$totalMemory = [math]::Round($computerInfo.TotalPhysicalMemory / 1GB, 2)
$operatingSystemName = $operatingSystem.Caption
$osVersion = $operatingSystem.Version

Write-Host "Manufacturer: $manufacturer"
Write-Host "Model: $model"
Write-Host "Total Memory (GB): $totalMemory"
Write-Host "Operating System: $operatingSystemName"
Write-Host "OS Version: $osVersion"

In the above code, we use the Get-WmiObject cmdlet to retrieve system information. We fetch information about the computer system using the Win32_ComputerSystem class and information about the operating system using the Win32_OperatingSystem class.

The example retrieves the manufacturer, model, total physical memory (in gigabytes), operating system name, and operating system version. It then displays the retrieved information using the Write-Host cmdlet.

Please note that you may need administrative privileges to run certain WMI queries depending on the system configuration.

User Management

Create a new user account

New-LocalUser -Name "JohnDoe" -Password (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) -FullName "John Doe" -Description "New User"

Modify user account properties

Set-LocalUser -Name “JohnDoe” -Description “Updated Description” -PasswordNeverExpires $true

Remove a User Account

Remove-LocalUser -Name "JohnDoe"

Enable or Disable a User Account

Disable-LocalUser -Name "JohnDoe"
Enable-LocalUser -Name "JohnDoe"

Add User to a Group

Add-LocalGroupMember -Group “Administrators” -Member “JohnDoe”

Remove User from a Group

Remove-LocalGroupMember -Group "Administrators" -Member "JohnDoe"

Please note that some of these commands may require administrative privileges to execute successfully. Adjust the user account name, password, and other parameters as needed for your specific use case.

List All Commands

Get-Command

List All Commands Beginning with A

Get-Command Get-a*

Replacing Spaces with an Underscore

Renaming files can be a laborious task, especially when there’s a lot to get through. This command renames all files within your current directory which have a space in the file name, and replaces it with an underscore, for example; ‘I need this.jpg’ changes to ‘I_need_this.jpg’.

Dir | Rename-Item –NewName { $_.name –replace “ “,”_” }

Listing File Names

This command will list all file names in the current directory and put it into a text file. The text file will be saved into the current directory.

ls > filename.txt

Conclusion

Harnessing the power of PowerShell snippets enables you to automate tasks, simplify complex operations, and enhance your overall productivity. By incorporating these pre-defined code blocks into your scripting workflow, you’ll save time and effort while maintaining code consistency and reusability. Whether you’re a PowerShell novice or an experienced user, mastering these snippets will make your scripting journey more efficient and rewarding.

Start exploring the world of PowerShell snippets today, customize them to fit your specific needs, and unlock the true potential of PowerShell for simplifying your daily tasks. With these invaluable code blocks at your disposal, you’ll be well-equipped to tackle any PowerShell scripting challenge with confidence.

Supercharge your PowerShell scripting with our comprehensive guide on essential snippets. Boost your productivity and automate with ease!

Leave a Reply

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