Getting Comfortable with PowerShell

I’m starting my journey with PowerShell. It seemed a rather interesting tool as presented on the MVA courses:

So, after the jump start, I decided to go for it and give it a good try for something else I’m passionate about besides scripting and automation. The Oracle Database.

That’s how my first PowerShell project was born.

But first, get comfortable with the ISE. Yes, you may want to use the PowerShell Integrated Scripting Environment if you, like me, are giving your first steps into PowerShell scripting.

Now, of course you won’t work directly on the path of the modules that are loaded when PowerShell is initiated. Thus, is a good idea to add a persistent path to the $env:PSModulePath environment variable, to do so, choose your path and add it as follows.

$CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
[Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + ";Drive:\Path\to\where\you\will\work\on\Modules", "Machine")

You can set this configuration for your local account only if you’re on a multi-user computer. Change the “Machine” scope to “User” scope as explained on this MSDN Article.

Now let’s get a better Prompt. Mine is like this, minimalist:

function Prompt {
$CurrentLocation=$($(Get-Location).ToString() -split '\\')[-1]
 "[$env:COMPUTERNAME] $CurrentLocation > "
}

This function is what you can modify to change the appearance and functionality of your prompt.

Now, you can get tired of getting this to load every time you run the PowerShell ISE, so you may want to automate this. Look for the $profile variable. Create a new file with that path and paste whatever you want done during the ISE startup.

[SOMEHOSTNAME] powershell > $profile
C:\Users\<Your-Username>\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

[SOMEHOSTNAME] powershell > if (-not $profile) { New-Item -Path $profile -ItemType File }

[SOMEHOSTNAME] powershell > psEdit $profile

Then paste something like this, substituting the paths for something that makes sense on your system

# Work or Autoload directory
$psScriptsDir="Drive:\Path\to\powershell\scripts"
# One line to load them all
Get-ChildItem -Path $psScriptsDir\Prompt.ps1 | %{.$_}
#Then change your location to your work directory
Set-Location Drive:\Path\to\powershell\work

Now you’re all set.

I’ll leave you with your first 5 commands!

  1. Update-Help: make sure you’re online. This will pull the latest docs for whatever you have installed and configured on your PS installation
  2. Get-Help: This will give you the initial step on how to use the help
  3. Find-Module: This command let’s you look for “keywords” on the PS Module Gallery. Try Find-Module *git* as an example.
  4. Save-Module: This can download the module and let you examine its contents. You can manually load the module.
  5. Install-Module: This command installs the module on your PS installation and makes it persistent, so it will be available every time you start PS

Happy scripting!

 

One thought on “Getting Comfortable with PowerShell

  1. Pingback: PowerShell + Oracle Client = Awesome Module – The Sides of My Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s