Skip to content

markgodiy/PUG

Repository files navigation

PowerShell User Group

Monad Manifesto

Back in Aug 8, 2002, Jeffrey Snover published the "Monad Manifesto", which articulated the long term vision and started the development effort which became PowerShell.

about_Powershell

Alt text

PowerShell is an object-based distributed automation engine, scripting language, and command line shell.

What is PowerShell? https://learn.microsoft.com/en-us/powershell/scripting/overview?view=powershell-5.1#next-steps

3 cmdlets beginners should know

1. Get-Command
2. Get-Member
3. Get-Help

# Use Get-Help to view the online guide and documentation for Get-Command and Get-Member

Get-Help Get-Command -online

Get-Help Get-Member -online

Quick Guide: Checking Running Services in PowerShell

⚠️ Running code found on the internet can pose significant security risks, including malware, data loss, and system instability. Exercise extreme caution, verify the source, and prioritize your system's security and privacy❗

Three common ways to write and execute PowerShell code:

  1. Copy, paste, and execute code.
  2. Create and call a function.
  3. Create and execute a .ps1 script file.

Example 1: Simple Copy and Paste

  1. Open PowerShell: Click the Start button, type "PowerShell," and press Enter.

  2. Copy and Paste: Copy the following command and paste it into the PowerShell window:

    Get-Service | Where-Object { $_.Status -eq 'Running' }
  3. Press Enter: Hit Enter to execute the command.

    • You'll see a list of all running services on your computer, neatly filtered to show only the ones that are currently active.

Example 2: Creating a Function

  1. Open PowerShell: Launch PowerShell if it's not already open.

  2. Create a Function: Copy and paste the same code snippet from Example1 and encapsulate it as a function within a pair of curly braces. Give the function a descriptive verb-noun name, e.g., Get-RunningServices:

    function Get-RunningServices {
        Get-Service | Where-Object { $_.Status -eq 'Running' }
    }
  3. Press Enter: Hit Enter to create the function. Nothing will happen, but that's okay. The function is loaded in session. We just need to call it to run the code.

  4. Run the Function: Now you can run your custom function anytime. Type Get-RunningServices at the prompt and press Enter.

    • The function will display a list of only the currently running services, making it easy to see what's active.

Example 3: Saving and Running a Script

  1. Create a PowerShell Script:

    • Open Notepad or any text editor.
    • Copy and paste the following code into the editor:
    # Save this as Get-RunningServices.ps1
    Get-Service | Where-Object { $_.Status -eq 'Running' }
  2. Save the File: Go to "File" > "Save As."

    • Choose a location and name the file as Get-RunningServices.ps1.
    • Save it.
  3. Adjust Execution Policy:

    • Open PowerShell as an Administrator (Right-click PowerShell and select "Run as Administrator").
    • Run the following command to allow the execution of PowerShell scripts:
    Set-ExecutionPolicy RemoteSigned
    • Confirm with "Y" (Yes) when prompted.
  4. Execute the Script:

    • Navigate to the directory where you saved Get-RunningServices.ps1.

    • Run the script by typing .\Get-RunningServices.ps1 and pressing Enter.

    • You'll see a list of running services, just like in Example 1, but with the added filter to show only the active ones.

Ready for more?

Microsoft used to publish a PowerShell scripting blog. It was retired a few years ago, sadly, but is still one of the best resources for practical examples of PowerShell. Check it out!

https://devblogs.microsoft.com/scripting/tag/scripting-guy/

About

PowerShell User Group

Resources

License

Stars

Watchers

Forks