Questions tagged [powershell-4.0]

For topics and problems related specifically to Windows PowerShell 4.0. PowerShell 4.0 shipped with the releases of with Windows 8.1 and Windows 2012 R2. It is available as a download for Windows 7 SP1. For general PowerShell topics, use the `PowerShell` tag.

For general PowerShell information see the tag wiki.

Description of PowerShell-v4.0

Windows PowerShell Microsoft's strategic task automation platform for Windows, and includes a command-line shell and scripting language. PowerShell was designed especially for system administrators.

PowerShell 4.0 ships with Windows 8.1 and Windows 2012 R2 as a built-in package.

Availability

PowerShell 4.0 is also available as a download for Windows 7 SP1. http://www.microsoft.com/en-us/download/details.aspx?id=40855

Notable New Features in 4.0

Resources

A list of fetaures new and changed int Windows PowerShell is available here.

For latest news and development, check out the PowerShell blog

1951 questions
44
votes
4 answers

Error Handling for Invoke-RestMethod - Powershell

I have a powershell script using the Skytap API (REST). I would like to catch the error, if there is one, and try to display it. For example, we are changing the IP: Invoke-RestMethod -Uri…
user01230
  • 563
  • 1
  • 6
  • 16
40
votes
2 answers

rename-item and override if filename exists

I am trying to use the Rename-Item cmdlet to do the following: I have folder which contains "*.txt" files. Let's say 1.txt, 2.txt etc... I want to copy files that have some prefix, rename them to *.txt and override any existing files if there are…
Ilya Gurenko
  • 545
  • 1
  • 5
  • 16
37
votes
5 answers

PowerShell string default parameter value does not work as expected

#Requires -Version 2.0 [CmdletBinding()] Param( [Parameter()] [string] $MyParam = $null ) if($MyParam -eq $null) { Write-Host 'works' } else { Write-Host 'does not work' } Outputs "does not work" => looks like strings are converted from…
D.R.
  • 20,268
  • 21
  • 102
  • 205
34
votes
3 answers

Output the date/time in PowerShell

I want to output the date time in various places in my script for logging so I am doing this: $b = Get-Date Write-Output "Backups complete at $b" # more code here $c = Get-Date Write-Output "Backups complete at $c" I am having to use multiple…
ErocM
  • 4,505
  • 24
  • 94
  • 161
28
votes
5 answers

How to change tab width when converting to JSON in Powershell

I am creating a JSON in Powershell and I want to set a custom tab width when building it (instead of the default 4 white spaces I want to set only 2 white spaces). I am doing this because: the actual JSON (not the one presented in the below…
Eduard Uta
  • 2,477
  • 5
  • 26
  • 36
27
votes
3 answers

Split a string with powershell to get the first and last element

If you do: git describe --long you get: 0.3.1-15-g3b885c5 Thats the meaning of the above string: Tag-CommitDistance-CommitId (http://git-scm.com/docs/git-describe) How would you split the string to get the first (Tag) and last (CommitId) element?
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
26
votes
5 answers

How to run the .reg file using PowerShell?

I want to run the .reg file (registry file) using PowerShell Script but I am not able to run it. When i run it manually it creates the respective nodes in registry but i want it execute using powershell script. Below is the code which i tried using…
SRP
  • 999
  • 4
  • 21
  • 39
25
votes
2 answers

Loading Assemblies from NuGet Packages

Sometimes in my PowerShell scripts, I need access to a specific DLL, using Add-Type -AssemblyName. However, the DLLs I need aren't always on the machine or in the GAC. For instance, I might want a quick script that uses Dapper to query a database.…
Travis Parks
  • 8,435
  • 12
  • 52
  • 85
24
votes
3 answers

PowerShell PSScriptRoot is null

When I run $PSScriptRoot it returns null. I am using PS version 4. $val = Join-Path -Path $PSScriptRoot WebPlatformInstaller_amd64_en-US.msi Error Join-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
kumar
  • 8,207
  • 20
  • 85
  • 176
24
votes
4 answers

How to set the exit code when throwing an exception

MyScript.ps1: exit 1 MyThrow.ps1: throw "test" Execution in PowerShell: & ".\MyScript.ps1" Write-Host $LastExitCode # Outputs 1 Clear-Variable LastExitCode & ".\MyThrow.ps1" Write-Host $LastExitCode # Outputs nothing How do I set a proper exit…
D.R.
  • 20,268
  • 21
  • 102
  • 205
21
votes
3 answers

Find numbers after specific text in a string with RegEx

I have a multiline string like the following: 2012-15-08 07:04 Bla bla bla blup 2012-15-08 07:05 *** Error importing row no. 5: The import of this line failed because bla bla 2012-15-08 07:05 Another text that I don't want to search... 2012-15-08…
Patric
  • 2,789
  • 9
  • 33
  • 60
21
votes
3 answers

Is the following possible in PowerShell: "Select-Object ."?

The scenario: I'm using Select-Object to access properties of a piped object, and one of those properties is itself an object. Let's call it PropertyObject. I want to access a property of that PropertyObject, say Property1. Is there any nice and…
Simon Elms
  • 17,832
  • 21
  • 87
  • 103
21
votes
1 answer

PowerShell XML SelectNodes fails to process XPath

I want to get a list of all project references in my csproj file using PowerShell. Currently I've the following approach: [xml]$csproj = Get-Content MyProject.csproj $refs = $csproj.SelectNodes("//ProjectReference") foreach($ref in $refs) { #…
D.R.
  • 20,268
  • 21
  • 102
  • 205
20
votes
3 answers

Powershell Invoke-RestMethod Authorization Header

While invoking an Invoke-RestMethod using Powershell like: Invoke-RestMethod -Method Get -Uri "https://google.com/api/GetData" -Headers $headers and $headers being $headers = @{ Authorization="Secret $username $password" …
20
votes
2 answers

How to check if an associative array is empty in powershell

$a = @() How do I check if $a above is empty (which it is). I would like to get $true as answer.
Spencer E
  • 203
  • 1
  • 2
  • 4
1
2 3
99 100