PowerShell – Environment variables

When working with PowerShell I’m searching for the same variables over and over again. Every time when I found them again I think the same “Oh yeah, I knew that!”. To minimize my search next time I thought about writing it down.

First question, how do I use these variables? This is pretty straightforward.

$Env:username

Second question, which environment variables can I use? Use the following cmdlet:

Get-Item Env: | sort Name

Name                           Value                                                                                      
----                           -----                                                                                      
COMPUTERNAME                   <computername>                                                                              
HOMEPATH                       Userssa-sp2010-Install                                                                   
LOCALAPPDATA                   C:Userssa-sp2010-InstallAppDataLocal                                                   
PSModulePath                   D:SPInstallSPModule;D:SPInstallSPModule;C:Userssa-sp2010-InstallDocumentsWindows...
PROCESSOR_ARCHITECTURE         AMD64                                                                                      
CommonProgramW6432             C:Program FilesCommon Files                                                              
CommonProgramFiles(x86)        C:Program Files (x86)Common Files                                                        
ProgramFiles(x86)              C:Program Files (x86)                                                                     
PROCESSOR_LEVEL                6                                                                                          
windows_tracing_flags          3                                                                                          
USERNAME                       sa-sp2010-Install                                                                          
HOMEDRIVE                      C:                                                                                         
USERPROFILE                    C:Userssa-sp2010-Install                                                                 
SystemRoot                     C:Windows                                                                                 
TEMP                           C:UsersSA-SP2~1AppDataLocalTemp                                                       
PUBLIC                         C:UsersPublic                                                                            
ALLUSERSPROFILE                C:ProgramData                                                                             
APPDATA                        C:Userssa-sp2010-InstallAppDataRoaming                                                 
ProgramData                    C:ProgramData                                                                             
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC                                      
OS                             Windows_NT                                                                                 
CommonProgramFiles             C:Program FilesCommon Files                                                              
PROCESSOR_IDENTIFIER           Intel64 Family 6 Model 23 Stepping 10, GenuineIntel                                        
ComSpec                        C:Windowssystem32cmd.exe                                                                
SystemDrive                    C:                                                                                         
PROCESSOR_REVISION             170a                                                                                       
ProgramFiles                   C:Program Files                                                                           
NUMBER_OF_PROCESSORS           2                                                                                          
windows_tracing_logfile        C:BVTBinTestsinstallpackagecsilogfile.log                                              
TMP                            C:UsersSA-SP2~1AppDataLocalTemp                                                       
ProgramW6432                   C:Program Files                                                                           
Path                           C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPower...
USERDOMAIN                     <DOMAIN>                                                                                      
VS100COMNTOOLS                 C:Program Files (x86)Microsoft Visual Studio 10.0Common7Tools                         
windir                         C:Windows

PowerShell – Get default calendar folder name

I was working on a script for monitoring the permissions of mailbox calendar folders, I had a big problem because its an Multilanguage exchange environment. Some of the mailbox folder names are in EN and some are in Dutch.

So I thought in the first place I can get the regional setting of the mailbox and based on that I can decide if it’s a Dutch or EN folder (Calendar or Agenda). But now I found out that some mailboxes got Dutch regional and EN folder names and vice versa.

This is how you can get the default calendar folder without checking the regional or language properties.

Get-mailbox | Get-MailboxFolderStatistics -FolderScope calendar | sort-object Name |ft Identity,Name

PowerShell – Open multiple tabs in IE

Here is a PowerShell script that you can use to open IE with multiple tabs on start-up:

$navOpenInBackgroundTab = 0x1000;
$ie = new-object -com InternetExplorer.Application
$ie.Navigate2("http://pkm-technology.nl");
$ie.Navigate2("http://khabbazi.nl", $navOpenInBackgroundTab);
$ie.Navigate2("http://khabbazi.nl/blog", $navOpenInBackgroundTab);
$ie.Visible = $true;

-Pouyan

Change Recover Deleted Items

If you want to change the default period of item retention ( Recover deleted items) from 14 to X days. To specify a value, enter it as a time span: dd.hh:mm:ss where d = days, h = hours, m = minutes, and s = seconds.

For example, to specify a 15-hour interval, enter 15:00:00. The maximum length of time to retain deleted items is 24,855 days. By default, deleted items are retained for 14 days. This attribute applies to all mailboxes in this mailbox database that don’t have their own item retention attribute set.

Through the EMC:

Go to the properties of the database->limits tab->”Keep Deleted Items for (days)”.

Through PowerShell:

Set-MailboxDatabase "YourDatabaseName" -DeletedItemRetention 7.00:00:00

PK

 

Installing PowerShell on Server Core

In this post I will take you through step-by-step how to install Windows PowerShell onto a Windows Server 2008 R2 Core edition instance. I will also add an optional element of making PowerShell your default shell for when you log in.

  1. First we will install .Net Framework 2 or above, Windows PowerShell, and Server Manager Cmdlets
    start /w ocsetup NetFx2-ServerCore
    start /w ocsetup MicrosoftWindowsPowerShell
    start /w ocsetup ServerManager-PSH-Cmdlets
    
  2. 2.Reboot your server and then try powershell.exe at the command line will ensure it is installed onto your system. To run PowerShell now you can just type “PowerShell” .
  3. Optional: You can optionally set PowerShell to be your default shell instead of cmd.  This can be done in two way: By opening RegEdit and editing a setting or you can set the registry setting via PowerShell by navigating to “CD HKLM:”.  Below I will paste the code snippet for editing through PowerShell and a screen shot showing where to edit i RegEdit.  The path for RegEdit can be determined by the code snippet.
    Set-ItemProperty "HKLM:SoftwareMicrosoftWindows NTCurrentVersionWinLogon" Shell "PowerShell.exe"
    
  4. Now when you log onto this machine PowerShell will be your default Shell. To make this more useful in the setup stages I always like to add a bit more to the code so it will load the Server Manager Cmdlets, which helps manage Roles and Features.
    Set-ItemProperty "HKLM:SoftwareMicrosoftWindows NTCurrentVersionWinLogon" Shell ‘PowerShell.exe –noexit –command "Import-Module ServerManager"’