Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell Script to Create a File and Create a Directory

PowerShell Script to Create Files and Create Folders

Conversion from VBScript.  Out-file?

Get Member?

 ♣

Our PowerShell Mission

Our mission is straightforward, to create a new-Item -type directory.

My learning agenda for PowerShell is as follows:
1) To appreciate -type (-ItemType) and -path parameters.
2) To interrogate the capabilities of an object with Get-Member.

Example 1 - Find all the Dlls

Pre-requisites

You need to have already installed a local copy of PowerShell.  If necessary search the internet for 'Microsoft PowerShell download'.  Note PowerShell RC2 is much changed from its predecessor, release candidate 1.

Instructions

  1. Save the following script to a text file with a .ps1 extension, for example: C: \Script\dll.ps1.
  2. Launch PowerShell and navigate to the folder where you saved the .ps1 file.  Incidentally, the Dos cd command works fine in PowerShell.
  3. To call for your script file, type at the command line PS> .\dll.ps1.
    Note 1. The rhythm of the command is:  dot slash filename.
    Note 2. You don't have to type PS>
     

# PowerShell script to list the DLL files under the system32 folder
Clear-Host
$Dir = Get-Childitem C:\windows\system32 -recurse
# $Dir |Get-Member
$List = $Dir | where {$_.extension -eq ".dll"}
$List | format-Table name

Learning Points

Note 1: Beginning a script with cls is one of my idiosyncrasies, it simply means clear the screen of any previous output (just as it does in DOS).  The hash symbol # means a remark, or do not process this line.

Note 2:  $Dir = Get-Childitem C:\windows\system32 -recurse
This command sets the variable $Dir to the path of the files that we seek.  You have probably guessed the purpose of the -recurse switch, to drill down to the sub-folders.  Get-childitem is often abbreviated to its alias, gci.

Note 3:  $List = $Dir | where {$_.extension -eq ".dll"}
$List is another variable whose purpose is to filter the output, as a result we get only files with .dll extension.  Pay particular attention to the construction $_. which means, in this pipeline.  Also observe that instead of the equals sign, PowerShell needs -eq.

Note 4:  One of PowerShell's features is the pipe symbol (|).  Most PowerShell scripts contain at least one pipe to control, or filter the output of the main command.  See more on the $_ variable.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

Example 2 - List Properties

The purpose of this script is to list the properties of the file object controlled by the $dir variable.  When we display the list, how much information do we want?  What file properties are available?  All is revealed by Get-Member.

cls
# PowerShell script to list the DLL files under the system32 folder
$Dir = Get-Childitem C:\windows\system32 -recurse
$Dir |Get-Member
# $List = $Dir | where {$_.extension -eq ".dll"}
# $List |ft fullname |out-file C:\scripts\dll.txt
# List | format-Table name

Example 3: PowerShell Create Folder with New-Item

The secret of mastering New-Item is to observe the rhythm, go with the flow of, -path -name -type.

# PowerShell New-Item Folder
Clear-Host
$Location = "E:\PowerShell\"
New-Item -path $Location -name "Ezine" -itemType "directory"

Note 5:  As usual with my -path examples, I introduce a variable mainly to remind you to change its value if you want the script to work on your machine!

Note 6:  The -itemType parameter can be abbreviated to plain -Type.  Also "Directory" does not have to be in quotes.

Note 7:  If the script succeeds PowerShell gives you a summary of what the script achieved, however, I still like to open Windows Explorer to checkout my new folder. 

Example 4: New-Item File

# PowerShell New-Item File
Clear-Host
$Location = "E:\PowerShell\Ezine"
New-Item -path $Location -name "No210" -type File

Note 8:  If your version of the script fails, start troubleshooting with value of $Location.

Note 9:  That it's a case of New-Item file by name and -type "file" by nature.  By that I mean check your -type (or -itemType) string value.

Summary of Creating Files and Creating Directories with PowerShell

file.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials:

PowerShell Home   •Shell Application  • New-Object   • PowerShell create shortcut

PowerShell Logon Script   • Map Network Drive   • PowerShell add printer   • PowerShell -com

Invoke-Expression   • Invoke-Command 

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: