Tag Archives: Page navigation

Move files to another directory Based on Filename with Powershell

Where this code required?

In many scenario, we will got the requirement for moving the specific files from the large number of files from one location to another location based some filter  on the file name. This code will helps with same situation. The below code will split the filename before “-” and compare the existing list “filterLists” to find 

STEP #1

Declare the souce and target path locations.

$srcpath = “C:\Powershelltest\Source”
$dstpath = “C:\Powershelltest\Destination”

STEP #2

Fetch all the child file list to make the loop to start the validation of each files.

$fileList = Get-ChildItem -Path $srcpath -Force -Recurse
foreach ($file in $fileList)
{
}

STEP #3

Second level of loop for validating whether filename is mathing with our input

foreach($filelist in $filterLists)
{
#Our Logic
}

Complete Code

############################################################
#Project : Move files to another directory by File name filter
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 
#E-Mail : mail2thiyaguji@gmail.com 
############################################################
#set Source and Destination folder location
$srcpath = "C:\Powershelltest\Source"
$dstpath = "C:\Powershelltest\Destination"
#Set the files name which need to move to destination folder
$filterLists = @("70022333","70022867")

#Get all the child file list with source folder
$fileList = Get-ChildItem -Path $srcpath -Force -Recurse
#loop the source folder files to find the match
foreach ($file in $fileList)
{
#checking the match with filterlist
foreach($filelist in $filterLists)
{
#$key = $file.BaseName.Substring(0,8)
#Spliting value before "-" for matching with filterlists value
$splitFileName = $file.BaseName.Substring(0, $file.BaseName.IndexOf('-'))

if ($splitFileName -in $filelist)
{
$fileName = $file.Name

Move-Item -Path $($file.FullName) -Destination $dstpath
}
}
}

Output

After Execution

What do you think?

I hope you have an idea of  Move files to another directory Based on Filename with Powershell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

How To Display GUI POP UP message box in PowerShell

One of the simplest way to build simple message boxes in PowerShell is to “borrow” from .NET COM objects. Basically you initialize the object, and then you need to create instances of the object and finally call the Popup method. These messagebox code may be familiar to anyone who has programmed in vbScript, or any of the Microsoft programming languages such as Visual Basic or C#. The boxes can look pretty good, even though the coding is a bit monster.

In simple, all it really takes is two lines. One line to create the Wscript.Shell COM object and one to invoke the Popup method. Before the execution, let we discuss about the syntax below

Syntax :

intButton = objShell.Popup(strText,[nSecondsToWait],[strTitle],[nType])

Parameters :

strText String value containing the text you want to appear
in the pop-up message box.
nSecondsToWait It’s an optional. Maximum length of time to display the pop-up message
box (in seconds, Optional, default=infinite)
strTitle Title text string, It’s an optional.
nType Type of buttons and icons (Numeric, Optional)
These determine how the message box is used.

Button Types : OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5,                         Yes = 6, No = 7

Icon Types : 16  – “Stop Mark” icon. ,  32 – “Question Mark” icon, 48 – “Exclamation Mark” icon.  , 64 – “Information Mark” icon.

 

The command will write the return value of the clicked button to the pipeline as shown below

Button the return value :1 – OK button ,2 -Cancel button , 3 – Abort button , 4 – Retry button ,5  – Ignore button  , 6 – Yes button  , 7 –  No button

Example #1:

#creating object os WScript
$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
#invoking the POP method using object
$wshell.Popup("Are you want to continue from here?",5,"Hello User?",48+4)

Script Explanation:

From the syntax, please find parameter values.

strText : Are you want to continue from here?
nSecondsToWait : 5 (wait for 5 second)
strTitle : Hello User?
nType : 48+4 (Exclamation Mark icon + Yes and No buttons)
IntButton : Get the selected input as show in below table

OUTPUT:

What do you think?

I hope you have an idea of  How to display a pop-up message box with PowerShell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.