Solved

Powershell Syntax

Posted on 2013-12-04
5
416 Views
Last Modified: 2013-12-06
Hello.  I hate to post this here as I'm sure it's simple and have been searching, but I am finding how to create a simple directory using PowerShell syntax.

My issue, getting the syntax right to append a new directory to the end of 26 directories that are all the same, but the end folder varies as it's done by state, and there is a folder within.  So essentially I need a wildcard path and then be able to append a new directory say "Doctest" at the end of each line, each directory is basically:

c:\test\vtest\az
c:\test, vtest\mo
c:\test\vtest\hi,

for example.  

So that is the most symplistic.  The other possibility, the directories will be like :

c:\test\vtest\az\result
c:\test, vtest\mo\result
c:\test\vtest\result.  


But I really want it to be
c:\test\vtest\az\result\Doctest
c:\test, vtest\mo\result\Doctest
c:\test\vtest\result\Doctest


I am trying to use PowerShell for this and I am very, very new to PowerShell and would like to script this task (as a good place to start) rather than create these folders manually.

Thoughts?  Thanks.
0
Comment
Question by:jnordeng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 385 total points
ID: 39697237
If you are sure about the end directory name is "Result" then you can use this code.. It will search all folders inside the C:\temp and if the end folder name is Result then it will create a folder inside it.
$Dir = Get-ChildItem C:\temp -Recurse | 
 ?{$_.PSIsContainer -and $_.Fullname -match "Result$"} |
	Select -ExpandProperty Fullname
$Dir | % {
	New-Item -ItemType Container -Path $_ -Name "Doctest" -Force
}

Open in new window

If you are unsure about the end directory name the try this.. It will search all folders inside the C:\temp to find all end folder name and then create a folder inside it.
$Dir = Get-ChildItem "C:\Temp" -Recurse | 
  ? {$_.PSIsContainer -and `
  !(Get-ChildItem $_.Fullname | ?{$_.PSIsContainer})} |
	Select -ExpandProperty Fullname
$Dir | % {
New-Item -ItemType Container -Path $_ -Name "Doctest"
}

Open in new window

0
 

Author Comment

by:jnordeng
ID: 39697370
Awesome, thank you.  I will give this a try in the morning.  Appreciate the help.
0
 

Author Comment

by:jnordeng
ID: 39699299
Works well, so rather than modify and insert within the path is there a way (syntax) to plop on the end if the rest of the path is the same but the second to last is different?  Thanks.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39701658
Second code should work that way right? if not can you provide an example of the directory structure?
0
 

Author Closing Comment

by:jnordeng
ID: 39701717
Thanks for your help, did the trick, now to come up with other things to try and start working on PowerShell Scripting. ;)
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
The viewer will learn how to count occurrences of each item in an array.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question