Tips On Improving My Craft

Good afternoon!

Recently I have written two scripts in order to make life easier. I would love to get some feedback on any/all things I can do to improve or tweak my code and continue improving.

First every morning I have to open some webpages, RDP, and a specific file so I wrote this script:

# Below opens the three webpages I need on the same Edge window
Start-Process Microsoft-Edge:https://www.website1.com/
Start-Process Microsoft-Edge:https://www.website2.com/
Start-Process Microsoft-Edge:http://www.website3.com/

# Below opens the RDP GUI
Start-Process -FilePath "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Remote Desktop Connection"

# Below opens the word document I need
Start-Process -FilePath "\\ShareDriveExample\Example\Example\Document.docx"

Secondly I was tasked with pulling the installed software information off of some of our servers. To make it easy I wrote this:

Invoke-Command -ComputerName "examplePC" -ScriptBlock {Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion | Format-Table –AutoSize} > C:\Users\UserExample\Desktop\Example.txt

The “problems” I am having currently are two websites do not have trusted certificates so I have to click Advanced-Proceed. I would like it to automatically do that for me but can’t figure out how. I am running PowerShell 5.1 so I don’t have access to a couple commands on 6.0

Also, the information pulled from the servers has a bunch of space between blocks of data. So I have to delete the whitespace to condense it for easy reading. Any tips on that?

I am still getting used to PowerShell so any constructive criticism is greatly appreciated!

Just some thoughts, in general, on your questions:

Untrusted sites - Can you create a local exception in Edge (not my daily browser) for those sites?

You could also create a .txt file, call it websites.txt, with the websites URLs on each line on the file. Set up your script to read the file and do a For Each to run your Start-Process. This would make it easier to edit the sites or add more later. And you don’t have to repeat yourself in the code.

Extra Whitespace - you might want to remove the format-table and write the output directly to a file. Then play with using -autosize to see if it is causing the issue. How about using Export-Csv and opening in Excel?

Great start, keep it up. Also, keep an original copy of this code somewhere safe. In 2 years, look back at it to see how far you have come. You’ll be surprised.

 

 

 

 

 

Assuming you trust the websites you could export the self-signed certificate and stick it in your Trusted Root Certification Authorities certificate store, then you won’t get an error.

For getting rid of the white-space you can use regular expressions to look for two or more consecutive spaces and replace with a single space

'why the big           space?' -replace "\W{2,}"," "

If the gaps are vertical write the file to a new file with no blank lines:

Select-String -Pattern ".+" -Path E:\temp\gaps.txt | Select-Object -ExpandProperty Line | out-file e:\temp\nogaps.txt