Article IT systems and administration
Bartłomiej Róg | 2022-01-05

PowerShell – 5 Most Frequently Used Commands/Scripts

PowerShell is a powerful tool that significantly streamlines and simplifies the work of administrators. It allows you to implement hundreds or even thousands of changes with a few-line script. After reading this article, you'll learn which PowerShell commands and scripts are most frequently used. Each command includes an example that you might use or have already used in practice.

ForEach or ForEach-Object Statement

A loop is invaluable for operating on multiple objects. It allows you to perform an action on each object in a given collection. It's worth noting that if you use ForEach at the beginning of a line, it's considered a statement. By placing it after the pipeline, PowerShell treats it as an alias for the ForEach-Object command. In short, these commands differ in speed and memory usage. ForEach is faster, ForEach-Object uses fewer resources. Sounds complicated? In practice, it's much simpler. For example:

#Script that changes the domain in the email addresses of all users in Active Directory. #In this example, ForEach is treated as a separate statement. Import-Module ActiveDirectory #Imports the Active Directory module. #It's necessary to run the Get-ADUser and Set-ADUser commands. $ADusers = Get-ADUser -Filter * #Creates the $ADusers collection. It contains all users in AD. #Foreach loop for the $ADusers collection. Each individual object is designated as $ADuser. foreach ($ADuser in $ADusers) { $email = $ADuser.samaccountname + '@domain.pl' #Create the $email variable. #This variable contains the username with the @domain.pl suffix added. #E.g., for user john.smith, it will be john.smith@domain.pl. Set-ADUser -Identity $ADuser.samaccountname -EmailAddress $email #Set the variable created above as the email address. } #The loop will execute for each user in the $ADusers collection.

and

#Script that changes the domain in the email addresses of all users in Active Directory. #In this example, I'll use ForEach-Object.Import-Module ActiveDirectory #Import the Active Directory module. #It is necessary to run the Get-ADUser and Set-ADUser commands. $ADusers = Get-ADUser -Filter * #Creating the $ADusers collection. The collection contains all users in AD. #Sending the contents of $ADusers to ForEach-Object using the pipeline. #In this case, the $_ variable is used instead of $ADuser to identify a single object in the collection. $ADusers | ForEach-Object{ $email = $_.samaccountname + '@domain.pl' #Creating the $email variable. #The variable contains the username with the @domain.pl suffix added. #e.g., for user john.smith it will be john.smith@domain.pl. Set-ADUser -Identity $_.samaccountname -EmailAddress $email #Setting the variable created above as the email address. } #The loop contents will be executed for each user in the $ADusers collection.

Import-Csv / Export-Csv                      

How limited would PowerShell be without the ability to read and write to files? The Import-Csv / Export-Csv commands are the most frequently used solutions for external data operations. Import-Csv allows you to import data from a CSV file into your script. Similarly, Export-Csv exports the data, saving it in CSV format.

Export-Csv:

#Script that exports a list of processes to a CSV file. $process = Get-Process #Create the $process variable. #The variable contains the list of processes. $process | Export-Csv -Path C:\temp\process.csv #Pass $process via pipeline | to the Export-Csv command. #The Export-Csv command saves the $process variable to C:\temp\ as the process.csv file.

Import-Csv:

#Script for importing phone numbers from a CSV file. Import-Module ActiveDirectory #Import the ActiveDirectory module. #Necessary to run the Set-ADUser command. $NumbersList = Import-Csv -Path C:\temp\numbers.csv #Import the numbers.csv file, located at C:\temp\numbers.csv, to $NumbersList. #Start a foreach loop. #The loop executes for each row in the numbers.csv file. foreach ($Number in $NumbersList){ Set-ADUser -Identity $Number.samaccountname -MobilePhone $Number.telephonenumber #Assigns a phone number from the imported file to the user in AD. }

Plik numbers.csv:

"samaccountname","telephonenumber" "test.user1","145345345" "test.user2","123123123" "test.user3","145445661" "test.user4","125345345" "test.user6","115213721" "test.user7","155345345" "test.user8","176876555" "test.user9","122533345" "test.user10","999999999"

Select-Object

A simple and extremely useful command that selects an object or its properties. The example below selects users who have logged in to their account within the last 30 days. You're interested in the username and last login date. – You can obtain these properties using Select-Object.

#Script that displays the account name and last login date of accounts logged in within the last 30 days. Import-Module ActiveDirectory #Import the Active Directory module. $Date = (get-date).adddays(-30) #Create the $Date variable. #This variable contains the date 30 days ago. Get-ADUser -properties * -filter {(lastlogondate -ge $Date)} | Select-Object samaccountname,lastlogondate #Select accounts whose lastlogondate is later than or equal to $Date. #Using Select-Object, the result will be displayed as: #samaccountname lastlogondate #account1 01/01/2021 01:01:01 #account2 02/01/2021 02:02:02 #...

Where-Object

Using Where-Object, you can select objects of interest based on their attributes. Often, the filter parameter takes over the functionality of the Where-Object command. Using a filter significantly reduces script execution time, but it's not available for every command. In such cases, Where-Object comes in handy.

#Script displaying accounts from the "Test" department Import-Module ActiveDirectory #Import the Active Directory module. Get-ADUser -Filter 'Department -eq "Test"' #Selecting accounts using the -Filter parameter Get-ADUser -Properties Department -Filter * | Where-Object Department -eq 'Test' #Selecting accounts using Where-Object. #In this case, the output of the Get-ADUser command is sent to Where-Object via a pipe.

Get-ChildItem

Get-ChildItem returns files located in the specified locations. You can use this command to search for files or check the properties of interest. Below is an example of use:

#Script displaying the contents of the $path folder, including subfolders. $path = 'C:\test\*.*' #The path variable contains the location to be searched. Get-ChildItem -Path $path -Recurse #With the -Recurse parameter, the contents of the $path folder are searched, including subfolders.

Summary of PowerShell Most Commonly Used Commands

The most frequently used commands vary depending on the IT solutions in the work environment. The above list is based on the experience of an IT administrator working on Windows Server systems. The script examples provided, after testing, can be easily implemented in a production environment. Do you also use PowerShell on a daily basis? Perhaps it's worth considering which commands you use most often?

Need IT support? Support Online is an outsourcing company that has been providing comprehensive IT services to businesses for over 20 years. We provide our services throughout Poland and abroad. We offer modern solutions based on our knowledge and experience:
- 24/7 helpdesk
- dedicated servers
- private cloud
these are just some of our offerings.

Contact us!

Bartłomiej Róg - IT administrator at Support Online
Related articles
Case study
| migrations and backups Case Study: Implementing Hardened Backup in Microsoft Azure. Read more
Article
| career Employee training and development in Support Online Read more
Article
| migrations and backups Veeam Hardened Repository – What is it and how does it protect backups against ransomware in 2026? Read more
Your IT.
our
support.
Contact us

The administrator of your personal data is Support Online sp. z o.o. Your personal data will be processed to respond to your inquiry and, if you consent, also to send the SOL newsletter. You can read about the detailed rules for personal data processing by our organization in our Privacy Policy.

FAQ

An IT company, or IT firm, deals with information technology in a broad sense. This includes, among other things:

  1. Software design and development: An IT company can create custom applications for other companies or software products for the mass market. Depending on their specialization, these can include mobile applications, desktop applications, web applications, or embedded systems.
  2. Consulting services: An IT company often provides experts to advise on implementing new technologies, optimizing business processes, or selecting appropriate technological solutions.
  3. Cloud solutions: Many IT companies specialize in implementing and managing cloud solutions, such as data storage, application hosting, and data analysis platforms.
  4. IT security: Protection against cyberattacks, security audits, implementing security policies, and network monitoring are just some of the IT companies' responsibilities in this area.
  5. IT infrastructure management: In this area, a company may manage servers, databases, networks, and endpoints.
  6. Technical Support and Service: An IT company may provide support for its own products or general IT support for other companies, managing their technology on a daily basis.
  7. Training: Many IT providers also offer training in software use and secure technology use.
  8. Hardware Solutions: Some IT companies may also provide and configure computer, server, or network hardware.

Depending on their specialization and size, an IT company may offer one, several, or all of the above solutions. When choosing a provider, it's important to thoroughly understand their services and tailor them to your individual needs.

At Support Online, we have been supporting companies for years with

  1. comprehensive user support (both on-site and remotely),
  2. we service computers, phones, tablets, and related network issues,
  3. we specialize in server administration: Windows, Linux/Unix,
  4. we support virtualizers such as KVM, Hyper-V, VMWare, and Proxmox,
  5. we support cloud services, particularly solutions such as Azure, Microsoft 365, and AWS,
  6. we monitor servers and devices on the internet,
  7. we consult on development, DRP, and support the stability of your business in the IT layer.

If you're looking for a good IT company, Support Online is the right place to grow your business.

It's worth using an IT company like Support Online when:

  1. You plan to implement new technologies or software in your company.
  2. You need specialized technology consulting.
  3. You want to optimize existing IT processes.
  4. You struggle with digital security issues.
  5. You need support in managing your IT infrastructure.
  6. You lack internal resources or expertise to implement certain technology projects.

Using external IT experts can bring benefits in terms of saving time and resources, and ensuring high-quality solutions.

Hiring an IT company like Support Online offers several key benefits over an IT freelancer:

  1. Support from the entire team: An IT company has a full team of specialists, from DevOps specialists and Cyber ​​Security Specialists to IT Helpdesk Specialists, who possess diverse skills and experience, enabling faster problem resolution and the implementation of more complex projects.
  2. Reliability and stability: IT companies have an established reputation and track record, which can translate into greater reliability and stability of services.
  3. Maintenance and support: An IT company can offer service contracts, warranties, and after-sales support, which may be more difficult to obtain from an individual freelancer.
  4. Resources: Companies have access to more resources, tools, and technologies that can accelerate and improve project execution.
  5. Long-term availability: The risk of a freelancer disappearing or changing careers is greater than the risk of a well-established company going out of business.

However, it's worth noting that the choice between a company and a freelancer depends on your specific needs and situation. If you value peace of mind and a quick response to unexpected problems, it is worth choosing an IT company such as Support Online.

Partnering with IT Support Online offers the following advantages:

  1. Professional IT outsourcing: The company guarantees high-quality IT outsourcing services for businesses of all sizes.
  2. Comprehensive IT support: IT Support Online provides comprehensive IT support that meets the diverse needs of businesses.
  3. Saves time and money: With our support, clients can focus on their core business activities while reducing the costs associated with information technology management.
  4. Serving a diverse range of businesses: The company specializes in serving both small and medium-sized enterprises and large corporations, demonstrating its flexibility and ability to adapt to diverse client needs.
  5. Leadership in IT outsourcing: The company is recognized as a leader in IT outsourcing, particularly in the Poznań and Warsaw regions.

By partnering with our company, IT Support Online, businesses can count on a high standard of service and professionalism at every stage of the relationship.

Free consultation
22 335 28 00