Basic Powershell – batch convert Word Docx to PDF

This will work for doc as well as docx files. $documents_path=”c:\doc2pdf” $word_app = New-Object -ComObject Word.Application # This filter will find .doc as well as .docx documents Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object { $document = $word_app.Documents.Open($_.FullName) $pdf_filename = “$($_.DirectoryName)\$($_.BaseName).pdf” $document.SaveAs([ref] $pdf_filename, [ref] 17) $document.Close() } $word_app.Quit()

Stop Powershell from exiting

You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post. One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit “C:\SomeFolder\SomeScript.ps1” Per-script Fix: Add a prompt for input to the end … Read more

PowerShell script to check the status of a URL

I recently set up a script that does this. As David Brabant pointed out, you can use the System.Net.WebRequest class to do an HTTP request. To check whether it is operational, you should use the following example code: # First we create the request. $HTTP_Request = [System.Net.WebRequest]::Create(‘http://google.com’) # We then get a response from the … Read more

How do I include a locally defined function when using PowerShell’s Invoke-Command for remoting?

You need to pass the function itself (not a call to the function in the ScriptBlock). I had the same need just last week and found this SO discussion So your code will become: Invoke-Command -ScriptBlock ${function:foo} -argumentlist “Bye!” -ComputerName someserver.example.com -Credential someuser@example.com Note that by using this method, you can only pass parameters into … Read more

PowerShell – Decode System.Security.SecureString to readable password

Here you go: $password = ConvertTo-SecureString ‘P@ssw0rd’ -AsPlainText -Force $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($password) $result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) $result P@ssw0rd

How to get N files in a directory order by last modified date?

Limit just some files => pipe to Select-Object -first 10 Order in descending mode => pipe to Sort-Object LastWriteTime -Descending Do not list directory => pipe to Where-Object { -not $_.PsIsContainer } So to combine them together, here an example which reads all files from D:\Temp, sort them by LastWriteTime descending and select only the … Read more