Antony's Logo

Antony's pages of stuff

  • Geek Home
  • HTML & Web Code
    • 2d Canvas
    • 3d Canvas
    • Javascript
  • Command Line
    • Windows CL
    • Windows CL Elevated
    • Windows Powershell
    • Unix & Linux
  • Other Stuff
    • Electronics
    • TV & Radio
    • Excel
    • Powerpoint
    • Word
  • Contact

F*@#ing PowerShell Continued



More Things I have found out about using PowerShell.



Tables.

A Script run from PowerShell command can be used to convert the file format from text to many other MS types

Use the following Table to identify the short code for file formats

 

 

Reading Material
https://learn-powershell.net
http://ss64.com/ps/

A PowerShell script is the equivalent of a Windows CMD or MS-DOS batch file, the file should be saved with a .ps1 extension.

An important find for me, is that in Command Line you can use %USERPROFILE% to get to your home directory this doesn't work for PowerShell. In PowerShell the direct comparison is $ENV:UserProfile .

To link to home directory you can use $home or the even simpler tilde (~)

Example of this is the following PowerShell script,
This script looks in the origin folder and converts any file with the extension .doc or docx etc to pdf. this effectively uses PowerShell to open word converts the documents and save them back as a pdf.


$origin = '~\Documents\doc2pdf'
$destination = '~\Documents\doc2pdf'

$word_app = New-Object -ComObject Word.Application

echo "Seeking changes in the source folders..."

Get-ChildItem -Path $origin -Filter *.doc? | ForEach-Object {

    if (-Not (Test-Path "$destination\$($_.BaseName).pdf")) {
        $document = $word_app.Documents.Open($_.FullName)
        $pdf_filename = "$destination\$($_.BaseName).pdf"   
        echo "$($_.FullName) converted $pdf_filename!"
        $document.SaveAs([ref] $pdf_filename, [ref] 17)
        $document.Close()
    } 
}


$word_app.Quit()

We can combine PowerShell in command line that is called from task scheduler. In this example I open PowerShell and search the folder for a text string, returning matching files with the relevant lines all formatted in a table.

NOTE this will need to be saved with .cmd extension to work


@PowerShell -ExecutionPolicy Bypass -noexit -Command Invoke-Expression $('$args=@(^&{$args} %*);'+[String]::Join(';',(Get-Content '%~f0') -notmatch '^^@powershell.*EOF$')) & goto :EOF
gci -path '$home\Documents\doc2pdf\' -filter '*' | Select-String -patt "34444" | select Filename, LineNumber | Format-Table -a
						

Jump Back to PowerShell and me on....

  • PowerShell

by Ant Monkey on Juice © 

Contact me here