#5 - PowerShell - Loops - Modules - PowerShell Galery

Inception

Hello everyone, This article is part of The PowerShell series, The knowledge in this series is built in sequence, Check out The PowerShell series.

In the last article, we discussed Using Pipelines, Variables, And Functions.

Today's Article will discuss Using Loops, Modules, PowerShell Galery, and Formating-output ✨


Using Loops

The for loop will run a block of code a specific number of times. This is useful for running the same code over and over, or for processing members of an array that match a particular characteristic.

Mastering-Windows-Server Page number: 213

The following example provides a practical overview:

$directory_content = get-childitem -path c:\  # collect directory content
# in a variable

# Let's loop in each line in The previous variable 
# and put it in $item variable

foreach ($item in $directory_content) {
  Write-output "Items: $item"  
  # Replace this command with any thing you want to do on each directory
  # For example create a file or copy a file
}
💡
Looking for more!? -Mastering-Windows-Server Page number: 213 -about_For
💡
will get more understanding while typing scripts together.

Modules

A module is a set of related Windows PowerShell functionalities, grouped together as a convenient unit.

understanding-a-windows-powershell-module

A module is a package that contains PowerShell members, such as cmdlets, providers, functions, workflows, variables, and aliases.

about-modules

As mentioned above, a Module is a package of PowerShell cmdlets Commands, and functions, approximately.

Modules provide you with new capabilities and commands. that enhanced by Microsoft itself or even the community, That toward you to a specific goals.

💡
The below example provides a practical example

How to Use a Module

To use a module, perform the following tasks:

  1. Install the module. (This is often done for you.)

  2. Find the commands that the module added.

  3. Use the commands that the module added.

about-modules

  • List installed Moudles:
Get-Modules

  • List modules that are Available in a default module location at your device, but not yet imported into your PowerShell terminal session, type:
Get-Module -ListAvailable
💡
Import a module from the list of available modules using Import-module <module-name>
  • To get all module commands of an existing module imported, Type:
Get-Command -Module Microsoft.PowerShell.Management

  • Use Get-Help to fetch a command syntax
get-help <Command>
  • Update the help file for a specific Module:
💡
each module has a help file that has the necessary help, while typing the get-help -module <module-name> you're fetching down this help file hosted on your local. sometimes this file content is old, so you need to update it to get the new help syntax that contains new options for example
Update-Help -Module <module-name>
  • To Import a module
Import-Module <module-name>
💡
you'll get more understanding in the below practical example.

PowerShell Galery

The PowerShell Gallery is the central repository for PowerShell content. it’s very similar to the Pipy repository for Python. In it, you can find PowerShell scripts, modules, and Desired State Configuration (DSC) resources. Some of these packages are authored by Microsoft, and others are authored by the PowerShell community.

That means it is very worthy if you don't find for example a command to achieve something, Search for it.

The following example provides a practical overview:

Search for folder size

  • Open it, and type the following command to install the module

      install-module -name <Module name>
    

  • list available Modules and filter the output that matches The module name "psfoldersize"
Get-Module -ListAvailable | ? {$_.name -match "foldersize"}
💡
tell now you installed the module. however, not imported at your powershell session (i.e. hosted in the list available modules on your local only)
  • Import the "psfoldersize" module
# Ensure first it's not imported
Get-Module

# import
Import-Module -Name PSFolderSize

# ensure that it's installed
get-module
  • Get The Module commands
Get-Command -Module PSFolderSize

Discover the module command

  • use the psfoldersize module command to get the folder size at the terminal in an easy way.
Get-FolderSize C:\psFolderSize_Test\Personal\


Formatting output

There's another very small thing we want to cover, The Formatting output; There are multiple ways to format the output command in The PowerShell Terminal.

You have the ability to customize the output of a command in various ways, you can Customize the command output as a table, list, or wide.

The following example provides a practical overview:

# Try out each one on them as view the diffrence 

Get-ChildItem | Format-Table
Get-ChildItem | Format-list
Get-ChildItem | Format-wide

Very Straightforward very Fast 🚀


In this article, we discussed Using Loops, Modules, PowerShell Galery, and Formating-output

Next, Will Create some Scripts and scenarios together ✨


References


That's it, Very straightforward, very fast🚀. Hope this article inspired you and will appreciate your feedback. Thank you.

0
Subscribe to my newsletter

Read articles from Mohamed El Eraki directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamed El Eraki
Mohamed El Eraki

Cloud & DevOps Engineer, Linux & Windows SysAdmin, PowerShell, Bash, Python Scriptwriter, Passionate about DevOps, Autonomous, and Self-Improvement, being DevOps Expert is my Aim.