2.1 - Introduction to PowerShell
Last updated
Last updated
PowerShell is a powerful scripting and automation platform developed by Microsoft. It combines the flexibility of a scripting language with the functionality of a command-line shell, enabling users to manage systems, automate repetitive tasks, and access system components programmatically.
Cross-Platform: Available on Windows, macOS, and Linux.
Object-Oriented: Operates with objects instead of plain text, allowing for more precise and flexible scripting.
Integration: Seamlessly integrates with .NET and other Microsoft technologies.
Use Cases: Ideal for administrative tasks, system configuration, and cybersecurity operations.
Scripts: .ps1
files containing PowerShell commands for automating tasks.
A script to list all running services:
Load a PowerShell script using dot sourcing:
Download execute cradle
Check out Invoke-CradleCrafter:
Load a PowerShell script using dot sourcing:
Modules: Packages of reusable cmdlets, scripts, and resources.
Popular modules include:
ActiveDirectory
: Manages AD objects.
Pester
: For testing scripts.
PSReadLine
: Enhances the shell experience.
Install with:
A module (or a script) can be imported using:
All the commands into a module can be listed using:
PowerShell is a primary tool for managing AD environments.
ADSI is a set of COM interfaces for interacting with directory services like Active Directory. It enables querying, modifying, and managing directory objects without requiring additional modules. PowerShell can use ADSI for lightweight operations, especially on systems without the AD module. ADSI uses LDAP paths to locate objects and execute changes. It is versatile but requires knowledge of directory structure and object attributes. Common use cases include user management, password resets, and retrieving directory information. ADSI is powerful for automation in legacy environments or restrictive setups.
.NET classes in PowerShell provide direct access to Active Directory operations via the .NET Framework. Key classes like DirectorySearcher
and DirectoryEntry
allow querying and managing directory objects. These classes offer fine-grained control over AD queries and modifications. Unlike cmdlets, .NET classes require coding expertise but are highly flexible. They are useful for advanced scenarios like custom searches or handling large datasets. .NET integration with PowerShell allows developers to bypass module dependencies. This approach is ideal for scripting in environments with strict constraints.
Native Executables
Native executables like dsquery
, net user
, and nltest
interact directly with Active Directory without PowerShell. They are pre-installed on Windows systems, making them a reliable fallback. These tools are useful for quick queries, user enumeration, or domain configuration checks. For example, dsquery
retrieves specific AD objects, while net user
checks group memberships. Native executables can be used in combination with PowerShell for hybrid scripts. They are particularly valuable in environments where PowerShell use is restricted. However, they are less script-friendly compared to PowerShell cmdlets or .NET classes.
4. WMI (Windows Management Instrumentation) with PowerShell
WMI is a Windows management framework for accessing system and network information, including Active Directory. PowerShell leverages WMI to query or modify system and directory data remotely. Common uses include querying system information, AD containers, or domain-related data. WMI supports cross-system communication, making it useful for remote administration. It uses namespaces and classes like DS_Container
to access directory objects. WMI is efficient for real-time monitoring and automation tasks. It is especially valuable in environments where standard AD tools are unavailable.
5. Active Directory Module (AD Module)
The AD module is a PowerShell module providing cmdlets for seamless Active Directory management. It simplifies complex AD operations, such as querying users (Get-ADUser
) or managing groups (Add-ADGroupMember
). The module integrates tightly with the AD schema, ensuring compatibility and ease of use. It is ideal for administrators who prefer high-level commands over low-level scripting. Common administrative tasks, like resetting passwords or managing group policies, are easily automated. The AD module requires installation on systems that need to run AD-specific cmdlets. It is a go-to tool for modern AD management via PowerShell.
Querying AD Objects:
Modifying Users or Groups:
Automation of repetitive administrative tasks.
Bulk modifications (e.g., importing users from CSV).
Real-time monitoring and auditing through queries.