What is PowerShell for ?
PowerShell is the next generation of the command line – a combination of a DOS style shell
and a scripting environment for Microsoft Windows.
PowerShell is not only for running DOS commands (which it does as well), it is much more flexible than that.
Unlike other text-based shells PowerShell language is similar to Perl. PowerShell harnesses the power of the .NET Framework,
providing a massive set of built-in functionality for taking control of your Windows environments.
PowerShell Console or PowerShell ISE
There are two styles of interface
Generally speaking powershell console in powershell 2.0 and earlier was MTA
and ISE was normally STA. since PowerShell 3.0, the PowerShell console runs in STA mode by default.
if you are working with non-GUI scripts, STA mode shouldn’t be an issue
but it is something that you should be aware of if you are creating GUIs tools.
Most GUIs will run fine in MTA mode, but as a general rule, it is recommended that you always run your GUI scripts in STA mode. To find out more about modes check out this link.
To check what mode PowerShell is configured, STA returns STA for the following command.
$host.Runspace.ApartmentState
If you are using PowerShell 2.0 or later, in MTA mode you can use the following command to start PowerShell with the STA option:
PowerShell.exe –STA
Windows PowerShell ISE always runs in STA mode.
To find out which version of PowerShell you have by using the following command (with the dollar sign).
$PSVersionTable
For PowerShell 1.0, this PSVersionTable variable does not exist, so if this variable is not available it is entirely safe to assume the engine is version 1.0.
Starting Windows PowerShell the newest version runs by default. To start Windows PowerShell with the Windows PowerShell 2.0 Engine, use the Version parameter of
PowerShell.exe. You can run the command at any command prompt, including Windows PowerShell and Cmd.exe.
To start PowerShell as a specific version you have use the following -Version command
PowerShell.exe -Version 2
As you automate your Windows operating system with PowerShell,
it helps to know how to create scripts that you may be able to loop and use more than once.
The steps to create a script follow:
Create the script in a plain text editor such as Notepad and save with a .PS1 file extension (e.g., myscript.ps1).
Run the script by entering the full path to the script (c:\scripts\myscript.ps1),
or if it’s in the current directory, prefix it with a period followed by a backslash (.\myscript.ps1).
If it happens to be in your parent directory this is prefixed using two periods and a backslash (..\myscript.ps1).
If the path to the script contains a space, enclose the full path in quotation marks
and prefix the entire thing with an ampersand (for example: &"C:\my scripts\myscript.ps1").
Unless you’re running a signed script, make sure you to set the correct execution policy using Set-ExecutionPolicy.
To allow local scripts to run unsigned, run:
Set-ExecutionPolicy RemoteSigned
How to View your Execution Policy
Get-ExecutionPolicy
Alternatively you can use:
Get-ExecutionPolicy -List | Format-Table -AutoSize
To set your Execution Policy use:
Set-ExecutionPolicy Unrestricted
At the time of writing this the latest full version of PowerShell is Version 4
PowerShell 2.0 is integrated in all Windows versions since Windows 7 and Windows Server 2008 R2.
If you want to ensure that a PowerShell script also works properly on a system with PowerShell 2.0,
you can switch to a PowerShell 2.0 prompt on every Windows version after Windows 7 with:
PowerShell.exe -Version 2
However, this will only work if you installed .NET Framework 2.0. Without .NET 2.0 you will get the error message: PowerShell.exe
and please note it also doesn’t work in PowerShell ISE
Trust!
PowerShell distinguishes between trustworthy folders and all other folders. You don't need to
provide the path name or append the file extension to the command name if the program is located in a trustworthy folder.
Commands like ping or ipconfig work as-is because they are in located a trustworthy folder.
The Windows environment variable Path determines whether a folder is trustworthy or not. All folders listed in this
environment variable are treated as "trustworthy" by PowerShell. You could put all your important programs in one of the
folders listed in the environment variable Path. You can find out this list by entering:
You can add other folders containing important programs to your Path environment variables, such as:
$env:path += ";C:\path\to\program" program.exe
Administrator!
To get Admin from From PS Command prompt
The PowerShell v2 way, according to Microsoft
Right click on the shortcut and choose Run as Administrator.
But to elevate within a PowerShell window:
start-process PowerShell –verb runAs
Which from a cmd.exe batch file, shortcut or Run line would look something (repetitively) like this:
powershell.exe -noprofile -NoExit -command "&{start-process powershell -ArgumentList '-NoExit -noprofile -file \"%~dp0psfile.ps1\"' -verb RunAs}"
Use the new PowerShell 4 command Get-FileHash <filepath> -Algorithm MD5
Get-FileHash C:\sample.txt -Algorithm MD5
Algorithm Hash Path
--------- ---- ----
MD5 BEA07E6D2B8DCE396FE21BAA61B34956 C:\sample.txt
PowerShell has lots of Special Characters and uses
$ (dollar sign) Describes variables that store state information for PowerShell. PowerShell also has variables maintained by PowerShell
$_ (dollar underscore) (Same as $PSItem which is only supported in PowerShell V3 and later). Contains the current object in the pipeline object.
You can use this variable in commands that perform an action on every object or on selected objects in a pipeline.
| (pipeline) Catches output of the command and passes it to another command.
One of PowerShell's greatest assets is the ability to pipe the output of one command into another command.
Operators $( ) @( ) :: &
As always ss64.com have it covered
% (percentage) ForEach-Object is aliased to the percetage sign
Get-Alias -Definition ForEach-Object
CommandType Name ModuleName
----------- ---- ----------
Alias % -> ForEach-Object
Alias foreach -> ForEach-Object
? (question mark) Output all items that conform with condition (Alias to Where-Object).
Get-Alias -Definition Where-Object
CommandType Name ModuleName
----------- ---- ----------
Alias ? -> Where-Object
Alias where -> Where-Object
-- (double hyphen) instructs PowerShell to treat everything coming after as literal arguments rather than options,
so that you can pass for instance a literal -foo to your script/application/cmdlet.
I know for me understanding when to use single or double quotation marks and back ticks etc has always been confusing so a good source of information regarding this can be found here understanding-quotation-marks-in-PowerShell
also worth looking at are here-strings
@ is the "array operator."
You can create an empty array:
Create an array with a single element:
$ThisArray = @("Hello World")
And you can import a CSV list and assign header properties
$ThisArray = Import-Csv .\array.csv -header @("ipaddress","hostname", "subnet","gateway")
We can then use a .dot command foreach $i in $ThisArray to get a property of $i by using something like $i.ipaddress ...
$ThisArray = Import-Csv .\array.csv -header @("ipaddress","hostname", "subnet","gateway")
ForEach ($i in $ThisArray) {
write-output ($i.ipaddress)
}
192.168.1.100
172.17.25.40
172.17.25.51
Jump to More PowerShell and me on....