PS – nslookup to serverlist

Hello together,

here i have a simple but nice snippet. This executes a “nslookup” in powershell on a list of computers.

Small but very effective:

$servers = get-content "path_to_the_file"
foreach ($server in $servers) {
$addresses = [System.Net.Dns]::GetHostAddresses($server)
foreach($a in $addresses) {
"{0},{1}" -f $server, $a.IPAddressToString



PS – Get-LastLinesFromFile

Hello together,

sometimes we want to open big files and read the data from it. And by big i mean really big. But many times we also only need some of the last lines – may be the newest ones. Herefore you can use the following script. By this way you do not load the whole file into cache first. This is fast and prevents also upcoming crashes.

   Gets last Lines of a file.
    Get-LastLinesFromFile -Path "c:\Temp\BigData.csv" -Last 20
function Get-LastLinesFromFile{
param ( $Path, 
      [int]$Last = 10, 
      [int]$ApproxCharsPerLine = 50
    $item = (Get-item $path) 
    if (-not $item) {return} 
    $Stream = $item.Open([System.IO.FileMode]::Open, 
    $reader = New-Object System.IO.StreamReader($Stream) 

    #Retrieving first set of Lines
    if ($charsPerLine * $last -lt $item.length) {
         $buf=$ * $last * $ApproxCharsPerLine) ,[System.IO.SeekOrigin]::End) 

    $LineCount=($content -Split "`n").Count
    $CharsMax = $last * $ApproxCharsPerLine
    $CharsMin = 0

    while ($LineCount -ne $Last)
        if ($LineCount -gt $Last)
            $content=$content -split "`n" -replace "\s+$","" | Select-Object -last $Last
            $LineCount=($content -Split "`n").Count
            #obsolete method to near to aiming number
            #new method has better performance
            #$CharsMin =$CharsMin+ ([Math]::Round(($CharsMax-$CharsMin)/2))
            #$ * ($CharsMax-$CharsMin),[System.IO.SeekOrigin]::End) 
            #$LineCount=($content -Split "`n").Count
            $CharsMax = $CharsMax+ ([Math]::Round(($CharsMax-$CharsMin)*2))
            $buf=$ * ($CharsMax-$CharsMin) ,[System.IO.SeekOrigin]::End) 
            $LineCount=($content  -Split "`n").Count


    return $content

Have fun with it.


PS – parameter by reference

Many developers know them – parameter by reference. By this way the variables will not be duplicated. A pointer is created which points to the actual variable. If you modify this referenced parameter you will modify the referenced variable. So you can make changes to many parameters and will not create a lot of functions with returnvalues.

[string]$Global = ''

function TestRefFunction([ref]$Param)
    [string]$Param.Value = '5'
    $Param | Out-Default


$Global | Out-Default

By this example the transmitted parameter “$global” is a reference. So all changes to it in the function will also be written into the global variable. In this example so 2 times a “5” will be given out.