Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, March 17, 2017 3:49 PM
This command
New-Item -Path "\\server\User\myname\[Profile Backup]\2017_03_17\" -Name "Installed_Programs.txt" -Force
is not creating the file. The target path does exist.
I've tried -Path with and with and without the trailing slash. I've also tried with and without -Type "file".
The command doesn't generate an error no matter the parameters or syntax and does not create the file.
If I navigate to the path with Windows Explorer, right click and select "New | Text Document" from the context menu I am able to create the the file.
Does anyone know what needs to be done so the New-Item command works?
A little more background, the full command is
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize | New-Item -Path "\\server\user\$Env:USERNAME\[Profile Backup]\$(Get-Date -Format yyyy_MM_dd)\" -Name "Installed_Programs.txt" -Force
And I've also tried
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize | Out-File -FilePath "\\server\user\$Env:USERNAME\[Profile Backup]\$(Get-Date -Format yyyy_MM_dd)\Installed_Programs.txt" -Force -Width 200
Out-File does show an error...
Out-File : Cannot perform operation because the wildcard path \server\User\myname\Profile Backup]\2017_03_17\Installed_Programs.txt did not resolve to a file.
However if I get $path=$("\server\user\Env:USERNAME\Profile Backup]\(Get-Date -Format yyyy_MM_dd)") and then put the value of $path into Windows Explorer address bar the target folder is opened.
UPDATE:
The following commands produce the correct path name including the square brackets. They do not create the output file though. The 1st version doesn't produce an error message and the output file is not created even if the full path is created beforehand. The 2nd version produces an error message and doesn't create the output file even if the full path is created beforehand.
~~~~~~~fails silently, no output, no error message~~~~~~~
Get-ChildItem | Get-Member | New-Item -Path $("\\fs1\user\$Env:USERNAME\`[Profile Backup`]\$(Get-Date -Format yyyy_MM_dd)\") -Name "Installed_Programs.txt" -ItemType File -Force
~~~~~~~fails with error message and no output~~~~~~~~~~~~
PS C:\> Get-ChildItem | Get-Member | Out-File -FilePath $("\\fs1\user\$Env:USERNAME\`[Profile Backup`]\$(Get-Date -Format yyyy_MM_dd)\Installed_Programs.txt") -Force
Out-File : Cannot perform operation because the wildcard path \\fs1\user\aboba\[Profile Backup]\2017_03_28\Installed_Programs.txt did not resolve to a file.
At line:1 char:30
+ Get-ChildItem | Get-Member | Out-File -FilePath $("\\fs1\user\$Env:USERNAME\`[Pr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (\\fs1\user\abob...ed_Programs.txt:String) [Out-File], FileNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
All replies (26)
Friday, March 17, 2017 4:39 PM | 1 vote
The issue is with the folder name [Profile Backup], if you remove the [] from the name it works without issue, if you keep them then the file does not get created.
If you run the command like below then it will work with that folder name
New-Item -Path "\\server\User\myname\[Profile Backup]\2017_03_17\Installed_Programs.txt" -Force
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Friday, March 17, 2017 5:26 PM
I tried and it didn't work for me.
I created a folder named "Profile Backup" on the specified path and still got the error when using Out-File and no error when using New-Item. The desired file is not created in either case.
Your reply also reminded me [ and ] are special characters so should be escaped to be interpreted as literals. Even with a ` (back tick) preceding the [ and ] the results are the same as described in my original post.
And, though I'm not sure why, I don't believe [ and ] where being interpreted as special characters.
$path=$("\server\user\Env:USERNAME\Profile Backup]\(Get-Date -Format yyyy_MM_dd)\) and
$path=$("\server\user\Env:USERNAME\[Profile Backup`]\(Get-Date -Format yyyy_MM_dd)\)
return the same value. The [ and ] don't seem to be interpreted as special characters in this case.
Friday, March 17, 2017 5:29 PM
I thought about escaping them also and could not get it to work, with your original script. If you use the updated script I supplied, then for some reason it works without issue.
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Friday, March 17, 2017 6:23 PM
New-Item -Name $filename -Path $path -ItemType File
\(ツ)_/
Friday, March 17, 2017 6:26 PM
Also you can force path creation:
New-Item -Name \server\User\myname\Profile Backup]\2017_03_17\ -ItemType File -Force
\(ツ)_/
Friday, March 17, 2017 6:30 PM
jrv,
I tested this with the following
New-Item -Path 'C:\Profile Backup]\2017_03_17\ -Name 'Installed_Programs.txt' -itemtype 'file' -Force
nothing gets created, Remove the [] from the folder name and update the path above and the file gets created.
The only way I could get the file created with keeping the [] in the name is doing
New-Item -Path 'C:\Profile Backup]\2017_03_17\Installed_Programs.txt' -itemtype 'file' -Force
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Friday, March 17, 2017 7:03 PM
Apologies clayman2, didn't read your post closely enough and recognize that you appended file name to -Path parameter.
Tried that and it does work as a standalone command.
When incorporated into my complete command it works a bit differently. Instead of simply creating the file a prompt "Type: " is displayed after pressing enter to perform the command.
I then type "file" and press enter again and the file is created with the expected content. If I add the "-Type file" parameter to the command the file is created, doesn't contain the expected output, and produces unexpected terminal output.
This causes the "Type: " prompt to be displayed and a file with expected content to be created...
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize | New-Item -Path "\\server\user\$Env:USERNAME\[Profile Backup]\$(Get-Date -Format yyyy_MM_dd)\Installed_Programs.txt" -Force
This creates the file which doesn't contain the expected output and produces unexpected terminal output (appended "-Type file" to command), ...
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize | New-Item -Path "\\server\user\$Env:USERNAME\[Profile Backup]\$(Get-Date -Format yyyy_MM_dd)\Installed_Programs.txt" -Force -Type file
Screen output from the 2nd command is...
Directory: \\server\user\myname\[Profile Backup]\2017_03_17
Mode LastWriteTime Length Name
-a 3/17/2017 2:51 PM 61 Installed_Programs.txt
-a 3/17/2017 2:51 PM 60 Installed_Programs.txt
-a 3/17/2017 2:51 PM 61 Installed_Programs.txt
-a 3/17/2017 2:51 PM 61 Installed_Programs.txt
-a 3/17/2017 2:51 PM 61 Installed_Programs.txt
-a 3/17/2017 2:51 PM 61 Installed_Programs.txt
...
...
...
-a 3/17/2017 2:51 PM 61 Installed_Programs.txt
The size of the file is 59 bytes and the only content is "Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"
Since this is being run interactively for now I can use the first form and type "file" when prompted.
Would still like to know how to get it to work without having to type "file".
Friday, March 17, 2017 7:16 PM
Do not pipe to Format-Table, this if needed should be the last command and is used for viewing in the console. If you try and pipe to it then pipe to another command, you run into many issues.
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Friday, March 17, 2017 7:24 PM
New-Item -Path "\\server\User\myname\``[Profile Backup``]\2017_03_17\" -Name "Installed_Programs.txt" -ItemType File -Force
New-Item -Path "\\server\User\myname\``[Profile Backup``]\$(Get-Date -Format yyyy_MM_dd)\" -Name "Installed_Programs.txt" -ItemType File
Friday, March 17, 2017 7:33 PM
Try to not scrunch all commands on one line. It is unreadable. The following is easier to type, easier to debug and readable. It is still a "one-line" command.
$key = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
$filepath = "\\fs1\user\$Env:USERNAME\[Profile Backup]\$(Get-Date -Format yyyy_MM_dd)\Installed_Programs.txt"
Get-ItemProperty $key |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table -AutoSize |
Out-String |
New-Item -Path $filepath -Force -ItemType file
You must use Out-String to send format commands to a fil.
\(ツ)_/
Friday, March 17, 2017 7:43 PM
Not running this in a script to get $filename and $path
However getting $filename and $path separately at the command line and substituting in the command didn't work either. Without the -ItemType parameter I'm prompted for "Type: ", enter "file" and no file is produced.
I've substituted as follows...
$filename="Installed_Programs.txt"
$path=$("\server\user\Env:USERNAME\Profile Backup]\(Get-Date -Format yyyy_MM_dd)\)
... New-Item -Path "$path$filename" -Force this prompts for Type but doesn't produce the file.
... New-Item -Path "$path"+"$filename" -Force this errors out
The following variations don't prompt for type in the first case and don't produce the odd terminal output I've described in the 2nd case. And the file is not created in either case.
... New-Item -Path "$path" -Name "$filename" -Force
... New-Item -Path "$path" -Name "$filename" -Force -ItemType File
Friday, March 17, 2017 7:54 PM
This works for me every time:
$key = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
$filepath = "d:\scripts\$Env:USERNAME\[Profile Backup]\$(Get-Date -Format yyyy_MM_dd)\Installed_Programs.txt"
Get-ItemProperty $key |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table -AutoSize |
Out-String |
New-Item -Path $filepath -Force -ItemType file
My post above had the wrong variable for path.
\(ツ)_/
Friday, March 17, 2017 7:57 PM
Did you see I use two back tick to escape in the path?
``[Profile Backup``]
Friday, March 17, 2017 7:59 PM
Did you see I use two back tick to escape in the path?
``[Profile Backup``]
"bactics" are not needed here. They are only needed with search commands.
"[]" should never be used in folder or file names for a great many reason.
\(ツ)_/
Friday, March 17, 2017 8:11 PM
Suggestion: Change the [] to {} until you get this working. The {} successfully makes the folder unreachable via the Net file API.
\(ツ)_/
Friday, March 17, 2017 8:20 PM
Also use the NetBIOS name of the computer. FQDN will not work.
\(ツ)_/
Friday, March 17, 2017 8:37 PM
Did you see I use two back tick to escape in the path?
``[Profile Backup``]
"bactics" are not needed here. They are only needed with search commands.
"[]" should never be used in folder or file names for a great many reason.
\(ツ)_/
You have very good suggestion. But in this case, I just tried to make the original code work. Using the path to the filename in one run works, but the question is to use both -path and -name inputs.
Friday, March 17, 2017 8:40 PM
You have very good suggestion. But in this case, I just tried to make the original code work. Using the path to the filename in one run works, but the question is to use both -path and -name inputs.
You can't with parameters and force. You can cat the path and name.
$filename = Join-Path $path $filename
But you can't use [].
\(ツ)_/
Friday, March 17, 2017 8:52 PM
I know what you mean. But I was playing with the question and it HAS [] in it.
Friday, March 17, 2017 8:59 PM
On WMF 5 the following will create both paths and the file but you still cannot use [] in the pat.
New-item -Path $path -Name $file -force
\(ツ)_/
Friday, March 17, 2017 9:05 PM
## This works
New-Item –Path "c:\temp\`[Profile Backup``]\ -Name "Installed_Programs.txt" -ItemType File -Force
#file created
C:\temp\Profile Backup]\Installed_Programs.txt
#This does not work
$path = "c:\temp\`[Profile Backup``]\
$file "Installed_Programs.txt"
New-item -Path $path -Name $file -ItemType File -force
Friday, March 17, 2017 9:21 PM
## This works
New-Item –Path "c:\temp\`[Profile Backup``]\ -Name "Installed_Programs.txt" -ItemType File -Force
#file created
C:\temp\Profile Backup]\Installed_Programs.txt
#This does not work
$path = "c:\temp\`[Profile Backup``]\
$file "Installed_Programs.txt"New-item -Path $path -Name $file -ItemType File -force
But the folder is inaccessible. Try to list it:
dir $path
\(ツ)_/
Saturday, March 18, 2017 2:31 AM
Like you said, don't use [] in folder and file names.
Here is the test script I have so far:
New-Item –Path "c:\temp\`[Profile Backup``]\ -Name "Installed_Programs.txt" -ItemType File -Forc
$path="c:\temp\Profile Backup]\Installed_Programs.txt"
notepad $path
Monday, March 20, 2017 8:12 PM
Regardless of including Format-Table or not the last cmdlet, New-Item, doesn't create the destination file, or produces undesired output (Friday, March 17, 2017 7:03 PM post).
Monday, March 20, 2017 8:15 PM
By default New-Item creates folders - not files.
Try reading the full help for a CmdLet when you don't understand it. It will save you a lot of wasted time.
\(ツ)_/
Tuesday, March 28, 2017 2:42 PM
From New Item help on MSDN
"Example 1: Create a file in the current directory"
So New-Item can be used to and does create files.