Yearly Archives: 2014

QuickTip: Change the local Administrator password on a remote server

To quickly change a password for any local administrator account, albeit locally or remote the following script can be used:

$account = [ADSI]("WinNT://server01/Administrator,user")
$account.psbase.invoke("setpassword",'Password1')

The WinNT provider allows for access to the SAM database on remote systems, assuming the account that executes this has the appropriate permissions to do so.

Share

QuickTip: Have the PowerShell console beep

Using the Beep method of the System.Console namespace it is possible to have the PowerShell console produce a beeping sound. Occasionally it can be useful to have the console get your attention by producing a sound. The following line of code will produce a short ‘beep’:

[System.Console]::Beep()

It is also possible to specify two parameters for this method, the first parameter is the tone measured in hertz and the second is the duration in milliseconds. Here is an example of a beep at 6000hz for half a second:

[System.Console]::Beep(6000,500)

for more information about this class, please refer to the MSDN entry of this method, which is available here:
Console.Beep method

Share

QuickTip: Convert .csv to .xlsx using Excel.Application

Using the Excel.Application Com-object converting a csv file to an xlsx file is quite simple, it can be written up in a single one-liner:

((New-Object -ComObject "Excel.Application").workbooks.open('c:\list.csv')).SaveAs('c:\list.xlsx',51)

Now this does leave an Excel process open so if you would like the process to be closed afterwards we could amend the code as follows:

$CurrentExcel = Get-Process Excel | Select-Object -ExpandProperty ID
((New-Object -ComObject "Excel.Application").workbooks.open('c:\list.csv')).SaveAs('c:\list.xlsx',51)
Start-Sleep 1
Get-Process Excel | Where-Object {$CurrentExcel -notcontains $_.ID} | Stop-Process -Force

By first gathering the list of all open Excel processes we can make sure that any previously opened Excel processes are not accidentally closed. That leaves us with a quick and easy piece of code to convert comma separated files to xlsx files on the fly.

For more information about the Excel.Application object have a look at its MSDN entry:
Application Object Excel

Share

QuickTip: Copy output to clipboard using clip.exe

Sometimes the contents of a certain variable or output of a script is quickly needed elsewhere, this is where the clipboard can be very useful. Since PowerShell supports pipelining to external tools as well as native cmdlets it is quite simple to output something to the clipboard, the following example will put the output of Get-Process on your clipboard:

Get-Process | clip.exe

Do you have any useful applications for this neat little trick, be sure to post them in the comment section.

Share

Active Directory Friday: Determine tombstone lifetime

In Active Directory objects are tomb stoned after a deletion occurs. This is allow replication to occur between domain controllers before an object is deleted from the Active Directory data store. The default value depends on the server when the forest was initially created, Microsoft recommends that this is set at 180 days.

The tombstone lifetime is set at the forest level and can be viewed by running the following code:

1
([adsi]"LDAP://CN=Directory Service,CN=Windows NT,CN=Services,$(([adsi](“LDAP://RootDSE”)).configurationNamingContext)").tombstoneLifetime

Alternatively this can also be retrieved by using the Get-ADObject cmdlet:

1
2
3
4
5
6
$HashSplat = @{
    Identity = 'CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=jaapbrasser,DC=com'
    Partition = 'CN=Configuration,DC=jaapbrasser,DC=com'
    Properties = 'tombstoneLifetime'
}
Get-ADObject @HashSplat | Select-Object -Property tombstoneLifetime
Share