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
Very good