DEV Community

Rupesh Tiwari
Rupesh Tiwari

Posted on • Originally published at rupeshtiwari.com on

PowerShell Random Notes for You!

Learn some important built-in very helpful methods in Powershell. It could be a quick note for you so donโ€™t forget to bookmark this article! ๐Ÿฅ‡

Try Catch in PowerShell

Using built-in exceptions.

try {
   $wc = new-object System.Net.WebClient
   $wc.DownloadFile("http://www.contoso.com/MyDoc.doc","c:\temp\MyDoc.doc")
}
catch [System.Net.WebException],[System.IO.IOException] {
    "Unable to download MyDoc.doc from http://www.contoso.com."
}
catch {
    "An error occurred that could not be resolved."
}

Enter fullscreen mode Exit fullscreen mode

Printing Anonymous Exception details.

try { NonsenseString }
catch {
  Write-Host "An error occurred:"
  Write-Host $_
  # will print the error details.
}


An Error occurred:
The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.

Enter fullscreen mode Exit fullscreen mode

Try Catch Finally in Powershell

try { NonsenseString }
catch {
  Write-Host "An error occurred:"
  Write-Host $_
  # will print the error details.
}
finally {
  Write-Host "I can move temporary log file to permanent location."
}

Enter fullscreen mode Exit fullscreen mode

if else case in Powershell

if(Boolean_expression) {
   // Executes when the Boolean expression is true
}else {
   // Executes when the Boolean expression is false
}

Enter fullscreen mode Exit fullscreen mode
  • -eq to check equality.
  • -le to check less than.

Example: Comparing Less than in Powershell

$x = 30

if($x -le 18){
   write-host "You can't get driving license!"
} else {
   write-host "You are eligible for Driving License!"
}

Enter fullscreen mode Exit fullscreen mode

Example: Comparing equality check

$file1Timestamp = [datetime](get-date -date '1989-09-25 8AM')

$file2Timestamp = [datetime](get-date -date '1989-09-25 10AM')

if($file1Timestamp -eq $file2Timestamp) {
    write-host "File is not modified"
} else {
    write-host "File is modified"
}

Enter fullscreen mode Exit fullscreen mode

Throwing custom error in Powershell

Function Do-Something
{
    throw "Your custom error"
    # This creates a runtime exception
    # that is a terminating error.
}

Enter fullscreen mode Exit fullscreen mode

Multiple line comment in Powershell

<#
Function Do-Something
{
Write-Host "The entire function is commented"
}
#>

Enter fullscreen mode Exit fullscreen mode

Contains vs Match vs Like in Powershell

Contains is used for collection and Tells whether a collection of reference values includes a single test value.

"abc" -contains "b" # return False

Enter fullscreen mode Exit fullscreen mode


 "abc","xyz" -contains "abc"

Enter fullscreen mode Exit fullscreen mode

Match is used in string and it uses regular expression to search in string.

"c:\temp\web\config.js" -match "b\\config.js" # True

Enter fullscreen mode Exit fullscreen mode

Like is used in string and it uses wild card search

"c:\temp\web\config.js" -like "*b\config.js" # True

Enter fullscreen mode Exit fullscreen mode

Whenever error occurs in batch file it save the error code in the environment variable %ERRORLEVEL%

echo "Return Code:" %ERRORLEVEL%

Enter fullscreen mode Exit fullscreen mode

Un-archive zip file

Using Expand-Archive cmdlet

Expand-Archive is built-in powershell 5 module.

 expand-archive -path c:\fsms\test.zip -destinationpath c:\fsms\unpack

Enter fullscreen mode Exit fullscreen mode

Using .Net class [System.IO.Compression.ZipFile]

ZipFile.ExtractToDirectory() is .Net Framework 4.5 built-in api.

[System.IO.Compression.ZipFile]::ExtractToDirectory("c:\fsms\test.zip","c:\fsms\unpack")

Enter fullscreen mode Exit fullscreen mode

Using Folder.CopyHere() Method of Shell.Application Class

Using COM object created from Shell.Application class. Use namespace().items() to get all items then copy them to destination folder. Make sure destination folder exist before you copy. Also this way of un-archiving files seems lil lengthy. However, I found that COM method to un-archive files are reliable since it is not modifying the modified date time of the files inside the zip file.

๐Ÿ† Pro Tip

For example if there is a file called as Web.config inside your web.zip\Content\website folder. Then if the actual modified date time is 5/4/2021 07:00:11 AM then this will show exact same time. However, if u use powershell cmdlet Expand-Archive or .Net ExtractToDirectory methods they will sometime display +1 hour so they will show file lastwritetime as 5/4/2021 08:00:11 AM. Note here it is showing 8AM rather 7AM.

Example:

$shell = new-object -com Shell.Application
$targetFolderToCopyUnzippedFiles = "c:\fsms\unpack"
# create a target folder if not exist. Make sure you have the folder otherwise un-archive will fail.
if(!(test-path $targetFolderToCopyUnzippedFiles)) {
  new-item -itemtype directory -path $targetFolderToCopyUnzippedFiles -force
}
$zipFile = "c:\fsms\test.zip"
# Unarchive and copy to target folder
$shell.namespace($targetFolderToCopyUnzippedFiles).copyhere($shell.namespace($zipFile).items(),4)

Enter fullscreen mode Exit fullscreen mode

References

If you know any other Powershell Built-In method that you use frequently then please write them in comment box. I may add those method details in this blog :slightly_smiling_face ๐Ÿ™‚


Thanks for reading my article till end. I hope you learned something special today. If you enjoyed this article then please share to your friends and if you have suggestions or thoughts to share with me then please write in the comment box.

Become full stack developer ๐Ÿ’ป

I teach at Fullstack Master. If you want to become Software Developer and grow your carrier as new Software Engineer or Lead Developer/Architect. Consider subscribing to our full stack development training programs. You will learn Angular, RxJS, JavaScript, System Architecture and much more with lots of hands on coding. We have All-Access Monthly membership plans and you will get unlimited access to all of our video courses, slides , download source code & Monthly video calls.

  • Please subscribe to All-Access Membership PRO plan to access current and future angular, node.js and related courses.
  • Please subscribe to All-Access Membership ELITE plan to get everything from PRO plan. Additionally, you will get access to a monthly live Q&A video call with Rupesh and you can ask doubts/questions and get more help, tips and tricks.

Your bright future is awaiting for you so visit today FullstackMaster and allow me to help you to board on your dream software company as a new Software Developer, Architect or Lead Engineer role.

๐Ÿ’– Say ๐Ÿ‘‹ to me!

Rupesh Tiwari

Founder of Fullstack Master

Email: rupesh.tiwari.info@gmail.com

Website: RupeshTiwari.com

Top comments (0)