Difference between revisions of "Powershell"

 
Line 1: Line 1:
List directories last modified older than 3 years ago <syntaxhighlight lang="powershell">
+
== List directories last modified older than 3 years ago ==
 +
<syntaxhighlight lang="powershell">
 
Get-ChildItem .\Projects | where{$_.LastWriteTime -lt (get-date).AddYears(-3)} | sort-object -Property LastWriteTime
 
Get-ChildItem .\Projects | where{$_.LastWriteTime -lt (get-date).AddYears(-3)} | sort-object -Property LastWriteTime
</syntaxhighlight>Load custom powershell profile
+
</syntaxhighlight>
  
To load a directory of custom posershell scripts automatically when you open a shell.  
+
== Load custom powershell profile ==
 +
To load a directory of custom powershell scripts automatically when you open a shell.  
  
first check if a PS profile exsits. if not create one<syntaxhighlight lang="powershell">
+
1) first check if a PS profile exists. if not create one<syntaxhighlight lang="powershell">
 
test-path $profile
 
test-path $profile
 
</syntaxhighlight>if this returns false create a profile like this<syntaxhighlight lang="powershell">
 
</syntaxhighlight>if this returns false create a profile like this<syntaxhighlight lang="powershell">

Latest revision as of 14:24, 11 September 2019

List directories last modified older than 3 years ago

Get-ChildItem .\Projects | where{$_.LastWriteTime -lt (get-date).AddYears(-3)} | sort-object -Property LastWriteTime

Load custom powershell profile

To load a directory of custom powershell scripts automatically when you open a shell.

1) first check if a PS profile exists. if not create one

test-path $profile

if this returns false create a profile like this

New-Item -path $profile -type file force

now open the the profile in notepad

notepad $profile

Now paste the following script changing the SCRIPT_DIR and START_DIR

$psdir = "SCRIPT_DIR"

Set-Location $psdir

#autoload script directory

$cnt = (Get-ChildItem $psdir).Count

For($i = 0; $i -le $cnt; $i++)

{ 

    Write-Progress -Activity "Loading Scripts" -status "Loading $i" -percentComplete ($i / $cnt*100)
    
}

Get-ChildItem "${psdir}\*.ps1" | %{.$_}

Set-Location c:\

Write-Host -foregroundcolor green "Custom Powershell Environment Loaded"