New-Item not seeming to work...

I've scaled down the relevant pieces into one little test script. Essentially, I'm defining a log file path/name in "$LOGFILE" and then in the Initialize-DebugLog function, if it is found, I delete the old one first, then I create a new file to start as a log file. It is not creating the file or logging the message to it. I had a slight variation of this working before, but now that I'm refactoring it, I'm having some issues.

Re: New-Item not seeming to work...

Re: New-Item not seeming to work...

The $PSScriptRoot variable doesn’t exist in PowerShell 2 and the log file is being written to C:\test.log

Nope...you can see from the first debug print out that the directory is on the front of the file name. That being said...I double checked and there is no file created in c:\ root.

Simon Sheppard wrote:

Your script works for me in PowerShell 5.1

There isn't anything version specific in there, is there? I just checked and I'm using PS V3.0 on this computer. Although the machines it will eventually run on seem to have v5.1. Maybe I should upgrade. Is PS a manual upgrade?

Re: New-Item not seeming to work...

Oops. Sorry to lead you down the wrong path. I put two and two together noticing that the window style in the image is reminiscent of Windows 7 and seeing $PSScriptRoot.

On closer inspection, it seems that New-Item fails because the path given to the `-Path` parameter contains brackets. PowerShell can often be a little… unpredictable when paths contain brackets. Usually a `-LiteralPath` would solve this problem but New-Item doesn’t support a `-LiteralPath` parameter.

One workaround would be to just change the current working directory to the target folder before issuing the New-Item command and omit the `-Path` parameter. Alternatively, you can leverage a .Net call to create the file:

Re: New-Item not seeming to work...

Pyprohly wrote:

Oops. Sorry to lead you down the wrong path. I put two and two together noticing that the window style in the image is reminiscent of Windows 7 and seeing $PSScriptRoot.

On closer inspection, it seems that New-Item fails because the path given to the `-Path` parameter contains brackets. PowerShell can often be a little… unpredictable when paths contain brackets. Usually a `-LiteralPath` would solve this problem but New-Item doesn’t support a `-LiteralPath` parameter.

One workaround would be to just change the current working directory to the target folder before issuing the New-Item command and omit the `-Path` parameter. Alternatively, you can leverage a .Net call to create the file:

[System.IO.File]::Create($LOGFILE)

Thanks...that sounds logical. I'll play with it keeping the brackets in mind. I was beginning to wonder if it was somehow a Onedrive weirdness issue, but the brackets makes more sense. I can easily just rename the directory to find out.

Right now I'm in the middle of upgrading to PowerShell 5.1, so I can see if that does anything different too.

BTW...it IS Windows 7. We're stuck on Win7 because of issues with the version of Clearcase that we use or something like that. Hoping they'll change that in the future and let me get to Win10 like I use at home.