Questions tagged [new-item]
39 questions
17
votes
4 answers
Create directory and file in same command using PowerShell
I try to create a folder and a file in one PowerShell command:
I tried:
New-Item hello\test\ (New-Item hello\test\file.txt -type file) -type directory -Force
and
New-Item file.txt (New-Item hello\test\ -type direcotry) -type file -Force
But both…

DenCowboy
- 13,884
- 38
- 114
- 210
5
votes
3 answers
Creating functions dynamically in a module in PowerShell
Suppose I have the following code in a module (called MyModule.psm1, in the proper place for a module):
function new-function{
$greeting='hello world'
new-item -path function:\ -name write-greeting -value {write-output $greeting} -Options…

Mike Shepard
- 17,466
- 6
- 51
- 69
2
votes
1 answer
PowerShell New-Item will create the directory where the script is and not in the path specified
I am writing a PowerShell script and I need to create a directory in C:\Users
My script is located on the desktop.
My line of script to create the directory is this:
New-Item -ItemType "directory" -Force -Path…

MyselfAndOnlyMe
- 33
- 1
- 3
2
votes
1 answer
Search Registry and create New-Item
I want to create a New-Item in the registry with a pre check what already exists.
This code
$items = get-item "HKCU:\SOFTWARE\Microsoft\Office\16.0\Excel\Options"
$items.Property -match "OPEN"
returns the…

aston_zh
- 6,543
- 4
- 20
- 23
1
vote
1 answer
PowerShell - string expansion inside a dynamically created function body?
I'm trying to dynamically generate functions in PowerShell that are intended to act similarly to command aliases in bash etc.
In essence the functions are supposed to be generated from a string array that specify the alias names.
e.g.:
$commands =…

so_below
- 15
- 4
1
vote
1 answer
How to apply condition to a string in the multiple string with powershell
I am trying to make a txt file and apply some content to a multiple string
this is what I did so far:
$Filename = "Jessica,Jimmy,Adam"
$Content = ({Whoisthesmartest=})
$Splitname = get-childitem $Filename -split ","
new-item c:\test.txt -itemtype…

DeGun999
- 37
- 2
1
vote
2 answers
Powershell. Is it possible to bind New-Item -Value parameter ByPropertyName?
Help New-item -parameter "Value"
show us -Value could be bind ByPropertyName or ByValue. The question is:
** Is it possible pipe ByPropertyName? **
As ByValue takes precedence over ByPropertyName, I can't find any example to be able…

Eduardo Fernández
- 25
- 5
1
vote
2 answers
Dynamically create functions from hashtable with parameters in PowerShell
Question
Hi, I'd like to dynamically create functions from a hashtable in Powershell. But the created functions are supposed to be able to receive parameters.
The Code I have so far:
$functions = @{
"wu" = "winget upgrade --include-unknown";
…

Dmitrij Drandarov
- 67
- 1
- 5
1
vote
2 answers
unexpected behavior when creating a path with %userProfile% environment variable in pwershell
I've written a script to create a series of symbolic links. I want to set the target value to $shortpath where
$shortpath = "%userprofile%\dir1\dir2\dir3\filename.ext"
The value of the $shortpath variable is valid and I can open it from the run…

Mlesko
- 21
- 2
1
vote
1 answer
PowerShell: Creating a set amount of files based on a parameter, with a running number to differentiate the files
So I have an assignment where I have to create an PowerShell script that takes three parameters, "$foldername", "$filename" and "$number".
The script checks if the folder "$foldername" exists and if not, creates it. After that it creates as many new…

Talar
- 13
- 3
1
vote
1 answer
I am trying to create a shortcut in Powershell from one server to another
$PrivateDrive = "Sharedrivepath1"
$ScanDrive = "ScanDrivePath2"
New-Item -Itemtype SymbolicLink -Path $PrivateDrive -Name ScanDrive -Value $ScanDrive
I am trying to create a shortcut from the ScanDrive to the PrivateDrive, I have a full filepath…

Fluffarmy223
- 47
- 1
- 5
1
vote
1 answer
Powershell: What's the right way to output full error messages from New-Item?
I'm running a vagrant winrm command, and am noticing that a command that fails doesn't print out the entire error output... I thought | might be used to expand output from such commands... but after some internet searching, and trying a few…

jayunit100
- 17,388
- 22
- 92
- 167
1
vote
1 answer
New-Item messing up my variable PowerShell
I wrote a very simple script to acquire a random free drive letter.
The function finds a random free letter , creates a new empty text file of that drive letter name eg. Q.txt
I then return the value as $new_letter but when it comes out of the…

Fenomatik
- 457
- 2
- 8
- 22
1
vote
1 answer
Cannot find path _ because it does not exist
I'm trying to create small script in powershell that would move files and directories to correct localizations. I made the following command:
Get-ChildItem -Path '.\list\' | ForEach-Object { if ($($_.Name) -like '*[1]*') {
$file = $($_.Name)
$path =…

plW0lf
- 13
- 1
- 3
1
vote
1 answer
How to use PowerShell to create a SymbolicLink for an exe with a file path parameter?
I am trying to create a Windows desktop shortcut (SymbolicLink) that runs a PowerShell script. If this were executed from the command line, it would be:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noexit -file…

Earthman55
- 33
- 5