Cmdlet parameters

If you wish to find cmdlet’s with a particular parameter, IE ComputerName, try the following PowerShell command:

PS> Get-Command -ParameterName computername

Sample output

CommandType     Name                             Version    Source
-----------     ----                             -------    ------
Cmdlet          Add-Computer               Microsoft.PowerShell.Management
Cmdlet          Clear-EventLog             Microsoft.PowerShell.Management
Cmdlet          Connect-PSSession          Microsoft.PowerShell.Core
[some lines deleted]
Cmdlet          Test-Connection            Microsoft.PowerShell.Management
Cmdlet          Test-WSMan                 Microsoft.WSMan.Management
Cmdlet          Write-EventLog             Microsoft.PowerShell.Management



Keywords: powershell cmdlet parameter

Posted in powershell | Leave a comment

UTF8 Encoding

I think more and more, I’m going to use UTF8 encoding for the work that I do both with Java and PowerShell.

The following code is a PowerShell summary (and a reminder) on creating UTF8 encoding objects.

## Test 1
Write-Host 'Test 1';
$utf8 = New-Object -TypeName System.Text.UTF8Encoding -ArgumentList $false, $true;
Write-Host ('Code page identifier = {0} ' -f $utf8.CodePage);
Write-Host ('Encoding Name = {0} ' -f $utf8.EncodingName);
$fred = $utf8.GetBytes('Hello World');


## Test 2

"*" * 35;

Write-Host 'Test 2';
$utf8_2 = [System.Text.Encoding]::GetEncoding(65001);
Write-Host ('Code page identifier = {0} ' -f $utf8_2.CodePage);
Write-Host ('Encoding Name = {0} ' -f $utf8_2.EncodingName);
$fred_2 = $utf8_2.GetBytes('Hello World');


## Test 3
"*" * 35;

Write-Host 'Test 3';
$utf8_3 = [System.Text.Encoding]::UTF8;
Write-Host ('Code page identifier = {0} ' -f $utf8_3.CodePage);
Write-Host ('Encoding Name = {0} ' -f $utf8_3.EncodingName);
$fred_3 = $utf8_3.GetBytes('Hello World');


# End of test

The resulting output is:

Test 1
Code page identifier = 65001
Encoding Name = Unicode (UTF-8)
Test 2
Code page identifier = 65001
Encoding Name = Unicode (UTF-8)
Test 3
Code page identifier = 65001
Encoding Name = Unicode (UTF-8)

The output HEX bytes show that the string “Hello World” are converted to the same bytes for each of the tests.
Keywords: utf8 encoding

Posted in powershell | Leave a comment

Java LIFO stack operations

I was doing Java work recently. In particular, using the Java class java.util.Stack as a LIFO mechanism. At the end of my program I iterated through my stack but couldn’t understand why if didn’t give me the last-in-first-out (LIFO) operation I was expecting. On searching the Internet I found that java.util.Stack iterates the wrong way. From this, I had to search for another Java collection to act as a stack.


A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.
For example:

Deque stack = new ArrayDeque();


JDK-4475301 : RFE: java.util.Stack.iterator() iterates the wrong way. It was an incorrect design decision to have Stack extend Vector (“is-a” rather than “has-a”). We sympathize with the submitter but cannot fix this because of compatibility.


Keywords: stack deque arraydeque java iterate



Posted in java | Tagged | Leave a comment

Endianness data storage

I was originally looking at generating random numbers in PowerShell by using the .NET class [System.Security.Cryptography.RandomNumberGenerator]. At some point I got sidetracked and wondered how two and four byte numbers were stored in my byte array. So hence the following code to look at at things.

function Show-Endian {

  # true if the architecture is little-endian;
  # false if it is big-endian.
  $endi = '';
  if ([System.BitConverter]::IsLittleEndian) {
      $endi = 'little endian';
  } else {
      $endi = 'big endian';
  Write-Output ('This computer appears to be {0}' -f $endi);

function Part-Two {

  [System.Byte]$my8 = 0;       # Represents an 8-bit unsigned integer.
  [System.UInt16]$my16 = 0;    # Represents a 16-bit unsigned integer.
  [System.UInt32]$my32 = 2041; # Represents a 32-bit unsigned integer.

  $data = New-Object Byte[] 4;

  Write-Output '';
  $mynum = $my32;

  Write-Output 'Unsigned bytes';
  Write-Output ('Maximum value: {0:X2}' -f [System.Byte]::MaxValue);
  Write-Output ('Minimum value: {0:X2}' -f [System.Byte]::MinValue);

  Write-Output '';
  Write-Output ('mynum is now decimal {0:N0}, hex {1:X2}' -f $mynum,$mynum);
  Write-Output ('data has size of {0} bytes' -f $data.Length);

  # convert the number in variable 'mynum' to the array
  # 'data'.
  $data = [System.BitConverter]::GetBytes($mynum);
  Write-Output '';
  Write-Output 'Looking at positions in the array'
  for ($i = 0; $i -lt $data.Length; $i++) {
      Write-Output ('Position {0} = {1:X2}' -f $i,$data[$i]);


$png = [System.Security.Cryptography.RandomNumberGenerator]::Create();
# -----
# -----
Write-Output 'End of test';


By running the script, we can see what’s happening.

This computer appears to be little endian

Unsigned bytes
Maximum value: FF
Minimum value: 00

mynum is now decimal 2,041, hex 7F9
data has size of 4 bytes

Looking at positions in the array
Position 0 = F9
Position 1 = 07
Position 2 = 00
Position 3 = 00
End of test

Hex 7F9 is stored in two bytes of our four byte array. F9 is stored in $data[0], and the remaining number, 7, is stored in $data[1]. Of course, for our test we could have used a two byte array to store the number used in our example.


The Wikipedia article at gives a good understanding of big-endian and little-endian.


Keywords: big-endian, little-endian, Endianness, byte, bitconverter



Posted in powershell | Leave a comment

Centre PowerShell forms

The .NET Framework class Form represents a window or dialog box that makes up an application’s user interface. In other words this is how we can build a GUI interface to an application with forms and dialog boxes.


The StartPosition property of the form is a way of being able to specify the initial starting position of a form. So this is useful if we wish to place the form in the centre of the screen. Use the following code to achieve this:


#Sets the starting position of the form at run time.
$CenterScreen = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form1.StartPosition = $CenterScreen;


The enumeration type System.Windows.Forms.FormStartPosition has several members to choose from and the one we’re interested in is CenterScreen. From the documentation, the description for this member is:


The form is centered on the current display, and has the dimensions specified in the form’s size.


If your PowerShell session does not seem to know about forms, then import the assembly.

[reflection.assembly]::loadwithpartialname(“System.Windows.Forms”) | Out-Null


The following loop will show the other members of System.Windows.Forms.FormStartPosition.


foreach ($m in [System.Enum]::GetValues([System.Windows.Forms.FormStartPosition]))
{ Write-Host ([int]$m): $m }


See also

How to Create and Use Enums in Powershell


Keywords: powershell form center centre start position





Posted in powershell | Tagged | Leave a comment

List lines from a text file

There may come a time when you want to display just a few lines from a text file. The following PowerShell code can be used to achieve this.

The examples will be based upon a sample text file containing the lines:



In this example, the parameter TotalCount of the Get-Content cmdlet specifies the number of lines from the beginning of a file. The default is -1 (list all lines).

Get-Content -Path myfile.txt -TotalCount 3

Sample output:


You can also achieve this with:

Get-Content myfile.txt -First 3


Doing this from the other end, the Get-Content cmdlet parameter Tail lists lines from the end of the file. This parameter specifies the number of lines from the end of a file.

Get-Content -Path myfile.txt -Tail 3

Sample output:



First and last few lines.

If you really wanted to, you could pipe the output from the Get-Content cmdlet into the Select-Object and make use of the First and Last parameters to look at the first few or last lines in the file.

  • First – Gets only the specified number of objects. Enter the number of objects to get.
  • Last – Specifies the number of objects to select from the end of an array of input objects.


Get-Content -Path myfile.txt | Select-Object -First 3


Get-Content -Path myfile.txt | Select-Object -Last 4

Lines in the middle of the file.

To look at lines other than the first or last, we could use the Select-Object parameter Index.

From the documentation:


Selects objects from an array based on their index values. Enter the indexes in a comma-separated list.

Indexes in an array begin with 0, where 0 represents the first value and (n-1) represents the last value. IE, the array is zero-based.

This means we have to subtract 1 from the line number(s) in question. To look at lines 4,5 and 6 inclusive, we have to specify line numbers 3,4 and 5.

Get-Content -Path myfile.txt | Select-Object -Index 3,4,5

will give you:


Of course, if you’d like to skip some lines, then use the following:

Get-Content -Path myfile.txt | Select-Object -Skip 4

Posted in powershell | Tagged , | Leave a comment

Enumerating over objects

Looping Through a Collection of Objects.

There are two ways of enumerating over objects. One may be considered easier than the other. Nevertheless the two ways are shown below.


Using a WHILE loop and GetEnumerator.

$lines = @('a', 'b', 'c', 'd', 'e', 'f');
$enumerator = $lines.GetEnumerator();

while ($enumerator.MoveNext()) {


Using a foreach loop.

$lines = @('a', 'b', 'c', 'd', 'e');
foreach ($item in $lines) {
   Write-Host $item;

See also

Foreach-Object Cmdlet

Posted in powershell | Tagged , | Leave a comment