50 commonly used PowerShell commands

 

50 commonly used Powershell commands

    Here’s are 50 commonly used PowerShell commands with explanation of the provided PowerShell cmdlets with examples. These commands are organized into categories for better understanding.

     

    Process and Service Management

    1. Get-Process
      Lists all running processes.

      # List all running processes
      Get-Process
      
      # Filter processes with a specific name
      Get-Process -Name "notepad"
      
    2. Get-Service
      Lists all services or details about a specific service.

      # List all services
      Get-Service
      
      # Check the status of a specific service
      Get-Service -Name "wuauserv"

     


    File System Operations

    1. Get-ChildItem
      Lists files and folders in a directory.

      # List all files and directories
      Get-ChildItem
      
      # Recursively list files in a directory
      Get-ChildItem -Path "C:\MyFolder" -Recurse
      
    2. Get-Content
      Reads the content of a file.

      # Read the content of a text file
      Get-Content -Path "C:\example.txt"
      
    3. Set-Content
      Writes or replaces content in a file.

      # Write content to a text file
      Set-Content -Path "C:\example.txt" -Value "Hello, World!"
      
    4. New-Item
      Creates a new file or directory.

      # Create a new directory
      New-Item -Path "C:\NewFolder" -ItemType Directory
      
      # Create a new file
      New-Item -Path "C:\example.txt" -ItemType File
      
    5. Remove-Item
      Deletes files or directories.

      # Delete a file
      Remove-Item -Path "C:\example.txt"
      
      # Delete a folder and its contents
      Remove-Item -Path "C:\NewFolder" -Recurse
      
    6. Rename-Item
      Renames a file or directory.

      # Rename a file
      Rename-Item -Path "C:\example.txt" -NewName "new_example.txt"
      
    7. Copy-Item
      Copies files or directories.

      # Copy a file
      Copy-Item -Path "C:\example.txt" -Destination "D:\Backup\example.txt"
      
    8. Move-Item
      Moves files or directories.

      # Move a file
      Move-Item -Path "C:\example.txt" -Destination "D:\example.txt"
      
    9. Test-Path
      Checks if a path exists.

      # Check if a file exists
      Test-Path -Path "C:\example.txt"

    Filtering and Iteration

    1. Select-Object
      Selects specific properties from objects.

      # Select specific properties of processes
      Get-Process | Select-Object Name, CPU
      
    2. Where-Object
      Filters objects based on conditions.

      # Get services that are running
      Get-Service | Where-Object {$_.Status -eq "Running"}
      
    3. ForEach-Object
      Performs an action for each object in a pipeline.

      # Stop all services that start with "A"
      Get-Service | Where-Object {$_.Name -like "A*"} | ForEach-Object {Stop-Service -Name $_.Name}
      
    4. If-Else
      Conditional execution of code.

      # Check if a file exists
      if (Test-Path -Path "C:\example.txt") {
          Write-Host "File exists"
      } else {
          Write-Host "File does not exist"
      }
      
    5. Switch
      Multi-condition evaluation.

      # Respond based on input
      $value = "B"
      switch ($value) {
          "A" { Write-Host "Value is A" }
          "B" { Write-Host "Value is B" }
          default { Write-Host "Unknown value" }
      }

    Help and Object Analysis

    1. Get-Help
      Displays help for a command.

      # Get help for Get-Process
      Get-Help Get-Process
      
    2. Get-Member
      Displays the properties and methods of an object.

      # Get members of a process object
      Get-Process | Get-Member
      
    3. Get-Command
      Lists all available commands or details about a specific command.

      # List all commands
      Get-Command
      
      # Get details of a specific command
      Get-Command Get-Process

    Data and File Manipulation

    1. Measure-Object
      Calculates properties of objects, like count or sum.

      # Count files in a directory
      Get-ChildItem -Path "C:\MyFolder" | Measure-Object
      
    2. Out-File
      Sends output to a file.

      # Save output to a file
      Get-Process | Out-File -FilePath "C:\processes.txt"
      
    3. Export-Csv
      Exports objects to a CSV file.

      # Export processes to a CSV file
      Get-Process | Export-Csv -Path "C:\processes.csv" -NoTypeInformation
      
    4. Import-Csv
      Imports data from a CSV file.

      # Import data from a CSV file
      $data = Import-Csv -Path "C:\processes.csv"
      
    5. ConvertTo-Csv
      Converts objects to CSV format (does not save to a file).

      # Convert data to CSV format
      Get-Process | ConvertTo-Csv

    Working with Properties

    1. Get-ItemProperty
      Gets properties of an item (like registry keys).

      # Get registry key properties
      Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion"
      
    2. Set-ItemProperty
      Sets properties of an item.

      # Modify a registry value
      Set-ItemProperty -Path "HKLM:\SOFTWARE\MyKey" -Name "MyValue" -Value "123"
      
    3. New-ItemProperty
      Creates a new property for an item.

      # Add a registry key property
      New-ItemProperty -Path "HKLM:\SOFTWARE\MyKey" -Name "NewValue" -Value "456" -PropertyType String
      
    4. Remove-ItemProperty
      Removes a property from an item.

      # Delete a registry key property
      Remove-ItemProperty -Path "HKLM:\SOFTWARE\MyKey" -Name "MyValue"

    Navigation and Environment

    1. Get-Location
      Gets the current directory.

      Get-Location
      
    2. Set-Location
      Changes the current directory.

      Set-Location -Path "C:\MyFolder"
      
    3. Clear-Host
      Clears the PowerShell console.

      Clear-Host

    Aliases

    1. Get-Alias
      Lists all aliases or a specific alias.

      Get-Alias
      
      # Get alias for Get-Process
      Get-Alias -Name "gps"
      
    2. New-Alias
      Creates a new alias.

      New-Alias -Name "list" -Value Get-ChildItem
      
    3. Remove-Alias
      Removes an alias.

      Remove-Alias -Name "list"
      

     


    Variables

    1. Get-Variable
      Lists all variables or a specific variable.

      Get-Variable
      
      # Get a specific variable
      Get-Variable -Name "Path"
      
    2. Set-Variable
      Creates or modifies a variable.

      Set-Variable -Name "MyVar" -Value "Hello, World!"
      
    3. New-Variable
      Creates a new variable.

      New-Variable -Name "NewVar" -Value 123
      
    4. **`Remove-

    Variable`**
    Removes a variable. powershell Remove-Variable -Name "NewVar"


    Remote and Event Management

    1. Invoke-Command
      Executes commands on remote machines.

      Invoke-Command -ComputerName "Server01" -ScriptBlock { Get-Process }
      
    2. Enter-PSSession
      Starts a remote session.

      Enter-PSSession -ComputerName "Server01"
      
    3. Exit-PSSession
      Ends the remote session.

      Exit-PSSession
      
    4. Get-EventLog
      Retrieves event log entries.

      Get-EventLog -LogName "Application"
      
    5. Clear-EventLog
      Clears event logs.

      Clear-EventLog -LogName "Application"


    Output and Logging

    1. Write-Output
      Sends data to the pipeline.

      Write-Output "Hello, World!"
      
    2. Write-Host
      Displays text in the console.

      Write-Host "Hello, World!" -ForegroundColor Green
      
    3. Write-Error
      Generates an error message.

      Write-Error "This is an error message."
      
    4. Write-Warning
      Generates a warning message.

      Write-Warning "This is a warning message."
      
    5. Write-Verbose
      Displays detailed information.

      Write-Verbose "Verbose output" -Verbose
      
    6. Write-Debug
      Displays debugging information.

      Write-Debug "Debugging information" -Debug
      
    7. Out-GridView
      Displays data in a grid view.

      Get-Process | Out-GridView

      Conclusion

      PowerShell cmdlets provide a versatile and powerful way to manage processes, services, files, directories, system settings, and even remote systems. With simple syntax and robust capabilities, these commands are essential for administrators and developers to automate tasks and streamline workflows.

      By understanding cmdlets like Get-Process, Get-Service, Copy-Item, and Where-Object, you can easily handle complex operations, filter and analyze data, and interact with the file system. Advanced cmdlets like Invoke-Command, Export-Csv, and Out-GridView further extend PowerShell’s functionality, enabling remote execution, data manipulation, and enhanced reporting.

      Whether you’re a beginner learning the basics or a professional managing intricate infrastructures, mastering these cmdlets equips you with the tools needed to efficiently handle day-to-day tasks and automate repetitive processes.

    admin
    admin

    Leave a Reply

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