Contents tagged with PowerShell

  • How to make alias for jar command

    Hello everybody,

    today I want to write few words about aliases in Powershell. First of all, why somebody may need aliases? Need in aliases arises, when you have some kind of big cumbersome commands. For example recently I've got a need to execute something like this:

    java -jar f:\utils\ANTLR\tpantlr2-code\code\antlr-4.7.1-complete.jar

    As you can see from the length, to type such a long string is pretty hard, even if you copy/paste it. In that case I've decided to use aliases. 

    My initial approach was like this:

    Set-Alias antlr4 'java -jar f:\utils\ANTLR\tpantlr2-code\code\antlr-4.7.1-complete.jar

    But when I've tried to execute such alias, Ive got the following error message:

    antlr4 : … more

  • Create group of users in PowerShell

    Hello everybody,

    today I want to share just simple script, with which you can create group of users according to some rules:

    $pw = "strangePassword" | ConvertTo-SecureString -AsPlainText -Force

    for($i=1

    $i -le 320

         $i++){

           $userName = "repeatingUserName" + $i

       New-ADUser -Name $userName -GivenName $userName -AccountPassword $pw -ChangePasswordAtLogon $false -PasswordNeverExpires $true -Enabled $true

               #Remove-ADUser -Identity $userName -Confirm:$false

    }

    more

  • PowerShell formatting strings

    Hello everybody,

    today I want to write few words about formatting strings in PowerShell.

    Consider the following example:

    "Today is $(Get-Date). Enjoy your life"

    Output will be the following:

    Today is 08/23/2016 16:04:18. Enjoy your life

    So, if you want to call some PowerShell function, you can just put it into brackets, put $ in front of it and get the result.

    Conisder another example:

    "You own to me $(44 + 44* 0.15) dollars"

    It will give following output:

    You own to me 50.6 dollars

    Very cool stuf in PowerShell is about using formatting with templating and key -f.

    Take a look:

    $apples = 5

    $pomegranade = 22

    cls

    "I have {0} apples and {1} … more

  • How to make pause in powershell script execution

    Hello everybody,

    today I want to make short note about how to make pause untill user press a key in PowerShell execution.

    Here it is:

      Write-Host "Press any key to continue ..."

     $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

    Presented code will make a pause. more

  • PowerShell built in variables

    Hello everybody,

    today I want to write few words about PowerShell variables.

    So, let's start from the simplest or with statement what is true and what is false. 

    In PS it's really not philosophical question

    $false

    $true

    The first one stands for false, and the second one for true. It is intended for logical operations in PS.

    Very usefull is Users Home directory:

    $Home

    Current directory:

    $pwd

    Information about users environment:

    $host

    If you ever need to discover which version of power shell, that you execute, you can use 

    $PSVersionTable

    Let's say that you write some kind of loop, and wonder where current item goes in, you have relieve, for this purpose you can use 

    $_ … more

  • PowerShell comparison

    Hello everybody,

    today I want to share another trick from PowerShell activities. 

    So, imagine, that you see following PowerShell code:

    "048" -eq 48

    48 -eq "048"

    What do you expect to see? True, True or True, False ....

    Here is the output:

    PS C:\sources> "048" -eq 48

    48 -eq "048"

    False

    True

    PowerShell has interesting behaviour in this case. It takes value on the right, and converts it to the value on the left. 

    For the first case PS will make following comparison:

    convert 48 to string, which will be "48". Compares "48" with "048" and produce False.

    more