Powershell Join-Path showing 2 dirs in result instead of 1 - accidental script/function outputWhy does a getter/setter property return duplicated value?Why is Select-String returning a DirectoryInfo object in addition to MatchInfo objects?Why does Range.BorderAround emit “True” to the console?UTF8 Script in PowerShell outputs incorrect charactersCreating a folder if it does not exists - “Item already exists”Creating subfolders with PowerShell in multiple parent foldersSplit-Path + Join-Path functionalityHow do I write a PowerShell script to test and copy file paths?Copy-Item and Join-Path creates a folder instead of a filePowerShell 5 Copy-Item not workingPowershell Copy-Item -DestinationUser input - folder and subfolder creation
Videos of surgery
What are the IPSE’s, the ASPE’s, the FRIPSE’s and the GRIPSE’s?
Why did James Cameron decide to give Alita big eyes?
Is it unusual for a math department not to have a mail/web server?
Many many thanks
Dual of a bimodule
Will removing shelving screws from studs damage the studs?
Could the UK amend the European Withdrawal Act and revoke the Article 50 invocation?
"Petrol aggregate"?
Can I use coax outlets for cable modem?
Why didn't Doc believe Marty was from the future?
Are strlen optimizations really needed in glibc?
Pen test results for web application include a file from a forbidden directory that is not even used or referenced
Commercial company wants me to list all prior "inventions", give up everything not listed
Time difference between banns and marriage
Is the Amazon rainforest the "world's lungs"?
How could a self contained organic body propel itself in space
How to determine algebraically whether an equation has an infinite solutions or not?
Does trying to charm an uncharmable creature cost a spell slot?
What's the point of fighting monsters in Zelda BoTW?
Why is there not a willingness from the world to step in between Pakistan and India?
Using a JoeBlow Sport pump on a presta valve
Toroidal Heyacrazy: Rainstorm
Why was this commercial plane highly delayed mid-flight?
Powershell Join-Path showing 2 dirs in result instead of 1 - accidental script/function output
Why does a getter/setter property return duplicated value?Why is Select-String returning a DirectoryInfo object in addition to MatchInfo objects?Why does Range.BorderAround emit “True” to the console?UTF8 Script in PowerShell outputs incorrect charactersCreating a folder if it does not exists - “Item already exists”Creating subfolders with PowerShell in multiple parent foldersSplit-Path + Join-Path functionalityHow do I write a PowerShell script to test and copy file paths?Copy-Item and Join-Path creates a folder instead of a filePowerShell 5 Copy-Item not workingPowershell Copy-Item -DestinationUser input - folder and subfolder creation
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am constructing incremental directory structures, and for some reason Join-Path is showing 2 dirs. When I later join that with a file I'm sending to copy-item, it causes an error, as shown below. I have shown in the comment for the $to_loc_finalDT1 line, where I first see these two dirs:
Copy-Item : Cannot find path '\T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv \T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv' because it does not exist
So this is the pertinent powershell script:
$T2 = "\T2DisasterBackupLoc"
$toLocParentDT2 = CreateDatedFolder $parentDirBaseNameDT2
$to_loc_finalDT2 = Join-Path -Path $toLocParentDT2 -ChildPath "Privileges"
#create sub-folder location
if(-Not (Test-Path $to_loc_finalDT2 ))
write-output " Creating folder $to_loc_finalDT2 because it does not exist "
New-Item -ItemType directory -Path $to_loc_finalDT2 -force
#second dir save files to
$parentDirBaseNameDT1 = "\T1DisasterBackupLoc"
$toLocParentDT1 = CreateDatedFolder $parentDirBaseNameDT1
$to_loc_finalDT1 = Join-Path -Path $toLocParentDT1 -ChildPath "Privileges" #shows 2 dirs here in debugger: \T2DisasterBackupLoc_2019-03-08Privileges \T2DisasterBackupLoc_2019-03-08Privileges
#create sub-folder location
if(-Not (Test-Path $to_loc_finalDT1 ))
write-output " Creating folder $to_loc_finalDT1 because it does not exist "
New-Item -ItemType directory -Path $to_loc_finalDT1 -force
I'm not sure how to get Join_path to just have the one dir, as it should. Right now, I think it's being treated as an array, which is not correct.
I tried searching for related issues, but didn't see anything similar.
Update
Here's the code for CreateDatedFolder:
#create dated folder to put backup files in
function CreateDatedFolder([string]$name)
$datedDir = ""
$datedDir = "$name" + "_" + "$((Get-Date).ToString('yyyy-MM-dd'))"
New-Item -ItemType Directory -Path $datedDir -force
return $datedDir
The output for that looks fine when it's returned. It appends the date onto the T2DisasterBackupLoc, but the debugger only shows one dir there, not an array or 2 dirs that are separate strings.
powershell return output return-value multiple-return-values
add a comment |
I am constructing incremental directory structures, and for some reason Join-Path is showing 2 dirs. When I later join that with a file I'm sending to copy-item, it causes an error, as shown below. I have shown in the comment for the $to_loc_finalDT1 line, where I first see these two dirs:
Copy-Item : Cannot find path '\T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv \T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv' because it does not exist
So this is the pertinent powershell script:
$T2 = "\T2DisasterBackupLoc"
$toLocParentDT2 = CreateDatedFolder $parentDirBaseNameDT2
$to_loc_finalDT2 = Join-Path -Path $toLocParentDT2 -ChildPath "Privileges"
#create sub-folder location
if(-Not (Test-Path $to_loc_finalDT2 ))
write-output " Creating folder $to_loc_finalDT2 because it does not exist "
New-Item -ItemType directory -Path $to_loc_finalDT2 -force
#second dir save files to
$parentDirBaseNameDT1 = "\T1DisasterBackupLoc"
$toLocParentDT1 = CreateDatedFolder $parentDirBaseNameDT1
$to_loc_finalDT1 = Join-Path -Path $toLocParentDT1 -ChildPath "Privileges" #shows 2 dirs here in debugger: \T2DisasterBackupLoc_2019-03-08Privileges \T2DisasterBackupLoc_2019-03-08Privileges
#create sub-folder location
if(-Not (Test-Path $to_loc_finalDT1 ))
write-output " Creating folder $to_loc_finalDT1 because it does not exist "
New-Item -ItemType directory -Path $to_loc_finalDT1 -force
I'm not sure how to get Join_path to just have the one dir, as it should. Right now, I think it's being treated as an array, which is not correct.
I tried searching for related issues, but didn't see anything similar.
Update
Here's the code for CreateDatedFolder:
#create dated folder to put backup files in
function CreateDatedFolder([string]$name)
$datedDir = ""
$datedDir = "$name" + "_" + "$((Get-Date).ToString('yyyy-MM-dd'))"
New-Item -ItemType Directory -Path $datedDir -force
return $datedDir
The output for that looks fine when it's returned. It appends the date onto the T2DisasterBackupLoc, but the debugger only shows one dir there, not an array or 2 dirs that are separate strings.
powershell return output return-value multiple-return-values
2
IsCreateDatedFolder
a function? What does it do? To me it looks likeCreateDatedFolder
creates two outputs that are stored in$toLocParentDT1
andJoin-Path
processes them both.
– T-Me
Mar 8 at 15:22
@T-Me - I added CreateDatedFolder to the Update of my question. It appends the date to the folder given. The output shows just one folder correctly, not the two.
– Michele
Mar 8 at 15:29
add a comment |
I am constructing incremental directory structures, and for some reason Join-Path is showing 2 dirs. When I later join that with a file I'm sending to copy-item, it causes an error, as shown below. I have shown in the comment for the $to_loc_finalDT1 line, where I first see these two dirs:
Copy-Item : Cannot find path '\T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv \T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv' because it does not exist
So this is the pertinent powershell script:
$T2 = "\T2DisasterBackupLoc"
$toLocParentDT2 = CreateDatedFolder $parentDirBaseNameDT2
$to_loc_finalDT2 = Join-Path -Path $toLocParentDT2 -ChildPath "Privileges"
#create sub-folder location
if(-Not (Test-Path $to_loc_finalDT2 ))
write-output " Creating folder $to_loc_finalDT2 because it does not exist "
New-Item -ItemType directory -Path $to_loc_finalDT2 -force
#second dir save files to
$parentDirBaseNameDT1 = "\T1DisasterBackupLoc"
$toLocParentDT1 = CreateDatedFolder $parentDirBaseNameDT1
$to_loc_finalDT1 = Join-Path -Path $toLocParentDT1 -ChildPath "Privileges" #shows 2 dirs here in debugger: \T2DisasterBackupLoc_2019-03-08Privileges \T2DisasterBackupLoc_2019-03-08Privileges
#create sub-folder location
if(-Not (Test-Path $to_loc_finalDT1 ))
write-output " Creating folder $to_loc_finalDT1 because it does not exist "
New-Item -ItemType directory -Path $to_loc_finalDT1 -force
I'm not sure how to get Join_path to just have the one dir, as it should. Right now, I think it's being treated as an array, which is not correct.
I tried searching for related issues, but didn't see anything similar.
Update
Here's the code for CreateDatedFolder:
#create dated folder to put backup files in
function CreateDatedFolder([string]$name)
$datedDir = ""
$datedDir = "$name" + "_" + "$((Get-Date).ToString('yyyy-MM-dd'))"
New-Item -ItemType Directory -Path $datedDir -force
return $datedDir
The output for that looks fine when it's returned. It appends the date onto the T2DisasterBackupLoc, but the debugger only shows one dir there, not an array or 2 dirs that are separate strings.
powershell return output return-value multiple-return-values
I am constructing incremental directory structures, and for some reason Join-Path is showing 2 dirs. When I later join that with a file I'm sending to copy-item, it causes an error, as shown below. I have shown in the comment for the $to_loc_finalDT1 line, where I first see these two dirs:
Copy-Item : Cannot find path '\T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv \T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv' because it does not exist
So this is the pertinent powershell script:
$T2 = "\T2DisasterBackupLoc"
$toLocParentDT2 = CreateDatedFolder $parentDirBaseNameDT2
$to_loc_finalDT2 = Join-Path -Path $toLocParentDT2 -ChildPath "Privileges"
#create sub-folder location
if(-Not (Test-Path $to_loc_finalDT2 ))
write-output " Creating folder $to_loc_finalDT2 because it does not exist "
New-Item -ItemType directory -Path $to_loc_finalDT2 -force
#second dir save files to
$parentDirBaseNameDT1 = "\T1DisasterBackupLoc"
$toLocParentDT1 = CreateDatedFolder $parentDirBaseNameDT1
$to_loc_finalDT1 = Join-Path -Path $toLocParentDT1 -ChildPath "Privileges" #shows 2 dirs here in debugger: \T2DisasterBackupLoc_2019-03-08Privileges \T2DisasterBackupLoc_2019-03-08Privileges
#create sub-folder location
if(-Not (Test-Path $to_loc_finalDT1 ))
write-output " Creating folder $to_loc_finalDT1 because it does not exist "
New-Item -ItemType directory -Path $to_loc_finalDT1 -force
I'm not sure how to get Join_path to just have the one dir, as it should. Right now, I think it's being treated as an array, which is not correct.
I tried searching for related issues, but didn't see anything similar.
Update
Here's the code for CreateDatedFolder:
#create dated folder to put backup files in
function CreateDatedFolder([string]$name)
$datedDir = ""
$datedDir = "$name" + "_" + "$((Get-Date).ToString('yyyy-MM-dd'))"
New-Item -ItemType Directory -Path $datedDir -force
return $datedDir
The output for that looks fine when it's returned. It appends the date onto the T2DisasterBackupLoc, but the debugger only shows one dir there, not an array or 2 dirs that are separate strings.
powershell return output return-value multiple-return-values
powershell return output return-value multiple-return-values
edited Mar 27 at 20:21
mklement0
154k26 gold badges275 silver badges319 bronze badges
154k26 gold badges275 silver badges319 bronze badges
asked Mar 8 at 15:03
MicheleMichele
1,5168 gold badges29 silver badges48 bronze badges
1,5168 gold badges29 silver badges48 bronze badges
2
IsCreateDatedFolder
a function? What does it do? To me it looks likeCreateDatedFolder
creates two outputs that are stored in$toLocParentDT1
andJoin-Path
processes them both.
– T-Me
Mar 8 at 15:22
@T-Me - I added CreateDatedFolder to the Update of my question. It appends the date to the folder given. The output shows just one folder correctly, not the two.
– Michele
Mar 8 at 15:29
add a comment |
2
IsCreateDatedFolder
a function? What does it do? To me it looks likeCreateDatedFolder
creates two outputs that are stored in$toLocParentDT1
andJoin-Path
processes them both.
– T-Me
Mar 8 at 15:22
@T-Me - I added CreateDatedFolder to the Update of my question. It appends the date to the folder given. The output shows just one folder correctly, not the two.
– Michele
Mar 8 at 15:29
2
2
Is
CreateDatedFolder
a function? What does it do? To me it looks like CreateDatedFolder
creates two outputs that are stored in $toLocParentDT1
and Join-Path
processes them both.– T-Me
Mar 8 at 15:22
Is
CreateDatedFolder
a function? What does it do? To me it looks like CreateDatedFolder
creates two outputs that are stored in $toLocParentDT1
and Join-Path
processes them both.– T-Me
Mar 8 at 15:22
@T-Me - I added CreateDatedFolder to the Update of my question. It appends the date to the folder given. The output shows just one folder correctly, not the two.
– Michele
Mar 8 at 15:29
@T-Me - I added CreateDatedFolder to the Update of my question. It appends the date to the folder given. The output shows just one folder correctly, not the two.
– Michele
Mar 8 at 15:29
add a comment |
1 Answer
1
active
oldest
votes
As T-Me correctly inferred before you posted the CreateDatedFolder
source, the problem is that the function inadvertently outputs 2 objects, and Join-Path
accepts an array of parent paths to each join with the child path.
Specifically, it is the New-Item
call that accidentally creates an additional output object, just before your return $datedDir
call.
New-Item
outputs a [System.IO.DirectoryInfo]
instance representing the newly created directory and, due to PowerShell's implicit output behavior, that instance becomes part of what the function outputs too - any command or expression inside a script / function that returns a value that isn't captured or redirected becomes part of the output.
To prevent that, suppress the output:
$null = New-Item -ItemType Directory -Path $datedDir -force
Note that you never need return
in PowerShell in order to output a result - but you may need it for flow control, to exit a function prematurely:
return $datedDir
is syntactic sugar for:
$datedDir # Implicitly output the value of $datedDir.
# While you could also use `Write-Output $datedDir`,
# that is rarely needed and actually slows things down.
return # return from the function - flow control only
For more information about PowerShell's implicit output behavior, see this answer.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065907%2fpowershell-join-path-showing-2-dirs-in-result-instead-of-1-accidental-script-f%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As T-Me correctly inferred before you posted the CreateDatedFolder
source, the problem is that the function inadvertently outputs 2 objects, and Join-Path
accepts an array of parent paths to each join with the child path.
Specifically, it is the New-Item
call that accidentally creates an additional output object, just before your return $datedDir
call.
New-Item
outputs a [System.IO.DirectoryInfo]
instance representing the newly created directory and, due to PowerShell's implicit output behavior, that instance becomes part of what the function outputs too - any command or expression inside a script / function that returns a value that isn't captured or redirected becomes part of the output.
To prevent that, suppress the output:
$null = New-Item -ItemType Directory -Path $datedDir -force
Note that you never need return
in PowerShell in order to output a result - but you may need it for flow control, to exit a function prematurely:
return $datedDir
is syntactic sugar for:
$datedDir # Implicitly output the value of $datedDir.
# While you could also use `Write-Output $datedDir`,
# that is rarely needed and actually slows things down.
return # return from the function - flow control only
For more information about PowerShell's implicit output behavior, see this answer.
add a comment |
As T-Me correctly inferred before you posted the CreateDatedFolder
source, the problem is that the function inadvertently outputs 2 objects, and Join-Path
accepts an array of parent paths to each join with the child path.
Specifically, it is the New-Item
call that accidentally creates an additional output object, just before your return $datedDir
call.
New-Item
outputs a [System.IO.DirectoryInfo]
instance representing the newly created directory and, due to PowerShell's implicit output behavior, that instance becomes part of what the function outputs too - any command or expression inside a script / function that returns a value that isn't captured or redirected becomes part of the output.
To prevent that, suppress the output:
$null = New-Item -ItemType Directory -Path $datedDir -force
Note that you never need return
in PowerShell in order to output a result - but you may need it for flow control, to exit a function prematurely:
return $datedDir
is syntactic sugar for:
$datedDir # Implicitly output the value of $datedDir.
# While you could also use `Write-Output $datedDir`,
# that is rarely needed and actually slows things down.
return # return from the function - flow control only
For more information about PowerShell's implicit output behavior, see this answer.
add a comment |
As T-Me correctly inferred before you posted the CreateDatedFolder
source, the problem is that the function inadvertently outputs 2 objects, and Join-Path
accepts an array of parent paths to each join with the child path.
Specifically, it is the New-Item
call that accidentally creates an additional output object, just before your return $datedDir
call.
New-Item
outputs a [System.IO.DirectoryInfo]
instance representing the newly created directory and, due to PowerShell's implicit output behavior, that instance becomes part of what the function outputs too - any command or expression inside a script / function that returns a value that isn't captured or redirected becomes part of the output.
To prevent that, suppress the output:
$null = New-Item -ItemType Directory -Path $datedDir -force
Note that you never need return
in PowerShell in order to output a result - but you may need it for flow control, to exit a function prematurely:
return $datedDir
is syntactic sugar for:
$datedDir # Implicitly output the value of $datedDir.
# While you could also use `Write-Output $datedDir`,
# that is rarely needed and actually slows things down.
return # return from the function - flow control only
For more information about PowerShell's implicit output behavior, see this answer.
As T-Me correctly inferred before you posted the CreateDatedFolder
source, the problem is that the function inadvertently outputs 2 objects, and Join-Path
accepts an array of parent paths to each join with the child path.
Specifically, it is the New-Item
call that accidentally creates an additional output object, just before your return $datedDir
call.
New-Item
outputs a [System.IO.DirectoryInfo]
instance representing the newly created directory and, due to PowerShell's implicit output behavior, that instance becomes part of what the function outputs too - any command or expression inside a script / function that returns a value that isn't captured or redirected becomes part of the output.
To prevent that, suppress the output:
$null = New-Item -ItemType Directory -Path $datedDir -force
Note that you never need return
in PowerShell in order to output a result - but you may need it for flow control, to exit a function prematurely:
return $datedDir
is syntactic sugar for:
$datedDir # Implicitly output the value of $datedDir.
# While you could also use `Write-Output $datedDir`,
# that is rarely needed and actually slows things down.
return # return from the function - flow control only
For more information about PowerShell's implicit output behavior, see this answer.
edited Jun 22 at 12:43
answered Mar 8 at 15:33
mklement0mklement0
154k26 gold badges275 silver badges319 bronze badges
154k26 gold badges275 silver badges319 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065907%2fpowershell-join-path-showing-2-dirs-in-result-instead-of-1-accidental-script-f%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Is
CreateDatedFolder
a function? What does it do? To me it looks likeCreateDatedFolder
creates two outputs that are stored in$toLocParentDT1
andJoin-Path
processes them both.– T-Me
Mar 8 at 15:22
@T-Me - I added CreateDatedFolder to the Update of my question. It appends the date to the folder given. The output shows just one folder correctly, not the two.
– Michele
Mar 8 at 15:29