9 sep 2013

create shortcut with powershell

 function Set-Shortcut ($linkpath , $targetPath )
{
    Write-Host $linkpath
    $strlinkpath = [String ]$linkpath
    if($targetPath -eq -$NULL)
    {
     throw ("targetpath not available" )
    }
    else
    {
        if(Test-Path $targetPath)
        {
        }
        else
        {
            throw " $targetPath does not exist"
        }
    }
    if($strlinkpath -eq $NULL)
    {     
            throw “linkpath not correct”
    }
    elseif($strlinkpath .EndsWith(".lnk" ))
    {
     $link            = ( New-Object -ComObject WScript.Shell). CreateShortcut($strlinkpath)
     $link.TargetPath = $targetPath
     ## Added after grawity's post
     $link.Save()
    }
    else
    {
        throw ("filename not available in path" )
    }

}
 

11 mrt 2013

script to generate files in folder structure


$DefaultPath = ""



$dirs =  get-childitem  -Recurse -Path $DefaultPath | where {$_.mode -match "d"}

foreach($dir in $dirs)
{
 $dirname =  $dir.FullName

    for ($i=1; $i -le 100; $i++)
    {
     $filename = "$dirname\file$i.txt"  
     write-host $filename
     New-Item $filename -type file -force -value "This is text added to the file"
    }
}