Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Syntax

Posted on 2013-12-04
5
Medium Priority
?
427 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
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 1540 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The viewer will learn how to count occurrences of each item in an array.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Suggested Courses

972 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