[FAQ CENTER トップ]
[Powershell FAQ トップへ]



【Powershell】Outlook,Excel,PowerPoint,Work,Access,IE,Explorerを起動しファイルを開き終了するサンプル



更新日:2022/03/13


PowerShell によりOutlook,Excel,PowerPoint,Work,Access,IE,Explorerを起動し、各アプリでファイルを開き終了するサンプルコードです。
アプリケーションを移動操作する場合などに活用してください。


#=== Start Outlook ===
$outlook = new-object -com Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$folder = $namespace.GetDefaultFolder("olFolderInbox")

$explorer = $folder.GetExplorer()
$explorer.Display()

#=== Start Excel ===
$excel = New-Object -ComObject Excel.Application
$book = $excel.Workbooks.Open("c:\temp\excel.xlsx")
$excel.Visible = $true


#=== Start PowerPoint ===
$ppt = New-Object -ComObject PowerPoint.Application
$powerpoint_document=$ppt.Presentations.open("c:\temp\powerpoint.pptx")

#$ppt.Visible = $true
$ppt.Visible =[Microsoft.Office.Core.MsoTriState]::msoTrue

#=== Start Word ===
$word = New-Object -ComObject word.application
$word_document=$Word.documents.open("c:\temp\word.docx")
$word.visible = $true

#=== Start Access ===
$access = New-Object -ComObject Access.application
$access.visible = $true



#=== Start エクスプローラ(explorer.exe) ===
#c:\tempでエクスプローラを開く
#方法1
Start-Process c:\temp

#方法2
Invoke-Item c:\temp

#=== IE を開く ===
$ie = New-Object -ComObject InternetExplorer.Application
#指定したサイトにアクセス
$ie.Navigate("google.com")
$ie.visible = $true


#ユーザが[Enter]を入力するのを待つ
Read-Host "Enterを入力すると起動したプログラムが終了します。"

$outlook.Quit()
$excel.Quit()
$ppt.Quit()
$access.Quit()
$word.Quit()
$ie.Quit()

#残存しているプロセスを終了する

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($namespace)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($folder)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($explorer)

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($book)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)


[System.Runtime.Interopservices.Marshal]::ReleaseComObject($powerpoint_document)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt)

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word_document)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($access)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)


[gc]::collect()









[Powershell FAQ トップへ]
[FAQ CENTER トップ]


本サイト内掲載されている情報は、著作権法により保護されています。いかなる場合でも権利者の許可なくコピー、配布することはできません。 このページはリンクフリーです。(このページへの直接リンクも可能です。) Copyright(c) tooljp.com 2007-2018