EN 
30.11.2025 Ondřej WELCOME IN MY WORLD

This website is originally written in the Czech language. Most content is machine (AI) translated into English. The translation may not be exact and may contain errors.

Tento článek si můžete zobrazit v originální české verzi. You can view this article in the original Czech version.
PowerShell - Ovládání aplikací a další použití

PowerShell - Application control and other uses

| Petr Bouška - Samuraj |
Another article about the useful (and sometimes necessary) Microsoft PowerShell tool. This time we'll cover some special use cases where we can manage applications like MS Word and Outlook or access Active Directory. In fact, PowerShell allows you to access COM objects or classes from the .NET Framework, the programming interfaces of various applications.
displayed: 15 954x (15 601 CZ, 353 EN) | Comments [1]
Here is the translation of the provided HTML code into English:

Microsoft Word

We must have the MS Word application installed. Then we can use the ProgID of the Word COM object and create an object. We can then perform a number of operations, such as opening documents, editing content, and setting parameters (including some for Outlook). Everything can happen hidden or publicly (in the example using visible).

$MSWord = New-Object -ComObject Word.Application
$MSWord.Visible = $true
# we can open an existing file or create a new one
# $MSWord.Documents.Open("d:\test.docx")
$WDoc = $MSWord.Documents.Add()
$WPar = $WDoc.Paragraphs.Add()
$WPar.Range.Text = "Test"
$WDoc.SaveAs([ref]"d:\test.docx")
$MSWord.Quit()

Microsoft Outlook

We can perform some Outlook settings through the Word.Application object. The example sets the signature for new messages (the signature must exist with this name).

$MSWord = New-Object -ComObject Word.Application
$MSWord.EmailOptions.EmailSignature.NewMessageSignature = "signature"
$MSWord.Quit()

For some functions, we have the Outlook.Application object. The example accesses the calendar and returns the first record (by default, access confirmation in the Outlook application is required).

$outlook = New-Object -ComObject Outlook.Application
$mapi = $outlook.GetNamespace("MAPI")
$calendar = $mapi.GetDefaultFolder("olFolderCalendar")
$calendar.Items.GetFirst()

Internet Explorer

We can work with Internet Explorer in the same way.

$ie = New-Object -ComObject InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate("www.samuraj-cz.com")
$ie.Quit()

Shell

The Shell.Application object gives us access to the system (shell). The examples perform - opening Windows Explorer with the specified folder, opening an address in the assigned browser, minimizing all open windows, and displaying a dialog to shut down the system.

$shell = New-Object -ComObject Shell.Application
$shell.Explore("c:\")
$shell.Open("https://www.samuraj-cz.com")
$shell.MinimizeAll()
$shell.ShutdownWindows()

Active Directory

For convenient access to AD, we can use the Active Directory Module for Windows PowerShell. But that means the computer where we want to run the script must have it installed. In some cases, we can't ensure that, but if we have the .NET Framework on the stations, we can use its capabilities. It contains the System.DirectoryServices Namespace, where the System.DirectoryServices.DirectorySearcher and System.DirectoryServices.DirectoryEntry classes are used to access AD DS.

In the example, we create an instance of the System.DirectoryServices.DirectorySearcher class, set a query to search for a user (the currently logged-in user, using an environment variable), and display their name and email. Using a cmdlet from the Active Directory module is simpler, but this method also works.

$Searcher = New-Object System.DirectoryServices.DirectorySearcher 
$Searcher.Filter = "(&(objectCategory=User)(samAccountName="+$Env:username+"))"
$ADUser = $Searcher.FindOne().GetDirectoryEntry()
$ADUser.DisplayName
$ADUser.mail

The second example shows listing the domain controllers. And there are many more possibilities.

$domain = [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain()
$domain.FindAllDomainControllers() | FT Name,SiteName,Roles -AutoSize
Author:

Related articles:

PowerShell

Articles related to Microsoft's PowerShell scripting language, which is used in all new versions of MS OS and applications.

If you want write something about this article use comments.

Comments
  1. [1] Jantar obecný

    Very good

    Friday, 03.12.2021 08:37 | answer
Add comment

Insert tag: strong em link

Help:
  • maximum length of comment is 2000 characters
  • HTML tags are not allowed (they will be removed), you can use only the special tags listed above the input field
  • new line (ENTER) ends paragraph and start new one
  • when you respond to a comment, put the original comment number in squar brackets at the beginning of the paragraph (line)